﻿using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using ayxjDllClass;
using System.Collections.Generic;
using System.Threading.Tasks;
using ServoTrack.WinForm;

namespace _6.jkxt
{
    public delegate void setText(Label lab, string str);
    public partial class Main : Form
    {
        public static CSName csName = new CSName();//引用名
        public static AGC agc = new AGC();
        int NowDate = 0;
        Process p = new Process();
        #region 初始化
        public Main()
        {
            InitializeComponent();
            if (Directory.Exists(csName.fileSatelliteData) == false) Directory.CreateDirectory(csName.fileSatelliteData);//如果不存在就创建file文件夹
                                                                                                                         //读多星历名,多星星历
            int L = 0;
            if (Directory.Exists(filePath))
            {
                DirectoryInfo TheFolder = new DirectoryInfo(filePath);
                foreach (FileInfo NextFile in TheFolder.GetFiles())//遍历文件
                {
                    csName.sxlm[L] = NextFile.Name;
                    using (StreamReader SR = NextFile.OpenText())
                    {
                        if ((csName.pl[2, 1] + ".txt") == NextFile.Name)//单
                        {
                            for (int i = 0; i < 3; i++) { csName.sXl[L * 3 + i] = csName.dXl[i] = SR.ReadLine(); }
                        }
                        else
                        {
                            for (int i = 0; i < 3; i++) { csName.sXl[L * 3 + i] = SR.ReadLine(); }
                        }
                        csName.sxlm[L] = csName.sXl[L * 3];
                    }
                    L++;
                }
            }
            //读单星时间段
            using (StreamReader dcrd = File.OpenText(Environment.CurrentDirectory + "\\text\\OneCl2.txt"))
            {
                for (int i = 0; ; i++)  //读入数据并赋予数组
                {
                    if (dcrd.EndOfStream) break; Array.Resize(ref csName.dtimfNew, i + 1); csName.dtimfNew[i] = dcrd.ReadLine();
                }
            }
            //读多星时间段
            using (StreamReader scrd = File.OpenText(Environment.CurrentDirectory + "\\text\\most2.txt"))
            {
                for (int i = 0; ; i++)  //读入数据并赋予数组
                {
                    if (scrd.EndOfStream) break; Array.Resize(ref csName.stimfNew, i + 1); csName.stimfNew[i] = scrd.ReadLine();
                }
            }
            //读记忆数据
            using (StreamReader jyS = File.OpenText(Environment.CurrentDirectory + "\\text\\jyNew.txt"))
            {
                for (int i = 0; ; i++)  //读入数据并赋予数组
                {
                    if (jyS.EndOfStream) break;
                    // Array.Resize(ref csName.jgNew, i + 1);
                    csName.jgNew[i] = jyS.ReadLine();
                }
            }
            //读取地面站
            using (StreamReader rd = File.OpenText(Environment.CurrentDirectory + "\\text\\satelliteParameter.txt"))
            {
                for (int i = 0; ; i++)
                {
                    if (rd.EndOfStream) break; string line = rd.ReadLine();
                    string[] st = line.Split('%');
                    Array.Resize(ref csName.satelliteName, i+ 1);
                    Array.Resize(ref csName.satelliteJd, i + 1);
                    Array.Resize(ref csName.satelliteJd1, i + 1);
                    Array.Resize(ref csName.satelliteJd2, i + 1);
                    Array.Resize(ref csName.satelliteJd3, i + 1);
                    csName.satelliteName[i] = st[0];
                    csName.satelliteJd[i] = Convert.ToDouble(st[1]);
                    csName.satelliteJd1[i] = st[2];
                    csName.satelliteJd2[i] = Convert.ToDouble(st[3]);
                    csName.satelliteJd3[i] = Convert.ToDouble(st[4]);
                }
            }
            //
            L = 0;
            using (StreamReader rd = File.OpenText(Environment.CurrentDirectory + "\\text\\deploy.ini"))
            {
                while (rd.EndOfStream == false)  //读入数据并赋予数组
                {
                    string line = rd.ReadLine();
                    string[] data = line.Split(':');
                    csName.pl[L, 0] = data[0];
                    csName.pl[L, 1] = data[1];
                    if (data[0] == "日志路径") { csName.pl[L, 1] = line.Substring(5); }
                    if (data[0] == "数据路径") { csName.pl[L, 1] = line.Substring(5); }
                    if (data[0] == "远端星历地址") { csName.SatelliteDataURL = csName.pl[L, 1] = line.Substring(7); }
                    L++;
                }
            }
            string[] wzXx = csName.pl[0, 1].Split(',');//天线位置
            wzWd.Text = wzXx[0];
            wzJd.Text = wzXx[2];
            wzHbg.Text = wzXx[4];
            tbclfs.Text = csName.pl[1, 1];//处理方式
            ipAddress.Text = csName.pl[4, 1];//IP地址
            port.Text = csName.pl[5, 1];//端口号
            mxTop.Text = csName.pl[6, 1];//通道上限
            mxBottom.Text = csName.pl[7, 1];//通道下限
            fwXz.Text = csName.pl[8, 1];//X轴修正值
            fyXz.Text = csName.pl[9, 1];//Y轴修正值
            fwMlSd.Text = csName.pl[10, 1];//X轴命令速度值
            fyMlSd.Text = csName.pl[11, 1];//Y轴命令速度值
            fwSd.Text = csName.pl[12, 1];//X轴扫描速度值
            fySd.Text = csName.pl[13, 1];//Y轴扫描速度值
            fwFw.Text = csName.pl[14, 1];//X轴扫描范围
            fyFw.Text = csName.pl[15, 1];//Y轴扫描范围
            inforVer.Text = csName.pl[16, 1];//连接校验
            timeBc.Text = csName.pl[17, 1];//程跟时间补偿
            timeSy.Text = csName.pl[18, 1];//数引时间补偿
            fwSx.Text = csName.pl[19, 1];//X轴顺限
            fwNx.Text = csName.pl[20, 1];//X轴逆限
            fySx.Text = csName.pl[21, 1];//Y轴上限
            fyXx.Text = csName.pl[22, 1];//Y轴下限
            gzMs.Text = csName.pl[23, 1];//跟踪模式
            boolYc.Text = csName.pl[24, 1];//是否远程
            rzLj.Text = csName.pl[25, 1];//日志路径
            sjLj.Text = csName.pl[26, 1];//数据路径
            gzJlJg.Text = csName.pl[27, 1];//记录间隔
            cmxSmSc.Text = csName.pl[28, 1];//扫描时长
            fwCg.Text = csName.pl[29, 1];
            AxisControl.setSpeesB(0, Convert.ToDouble(csName.pl[29, 1].Split(',')[0]));//轴角速比
            AxisControl.setSpeesB(1, Convert.ToDouble(csName.pl[29, 1].Split(',')[1]));//轴角速比
            AxisControl.setSpeesB(2, Convert.ToDouble(csName.pl[29, 1].Split(',')[2]));//轴角速比
            fyCg.Text = csName.pl[30, 1];
            fwSy.Text = csName.pl[31, 1];
            fySy.Text = csName.pl[32, 1];
            fwSf.Text = csName.pl[33, 1];
            fySf.Text = csName.pl[34, 1];
            dwJd.Text = csName.pl[35, 1];
            csName.dwJd = csName.pl[35, 1].Split(',');
            csName.fWjD = double.Parse(csName.pl[36, 1].Trim());
            toolStripTextBox4.Text = csName.pl[40, 1];//极化轴命令速度值
            toolStripTextBox2.Text = csName.pl[41, 1]; //极化顺限
            toolStripTextBox3.Text = csName.pl[42, 1]; //极化逆限
            toolStripTextBox1.Text = csName.pl[43, 1]; //极化偏移
            csName.Mode = Convert.ToInt32(csName.pl[45, 1]);
            toolStripTextBox5.Text = csName.pl[47, 1];
            toolStripTextBox6.Text = csName.pl[48, 1];
            if (csName.pl[24, 1] == "是")
            {
                ipAddress.Enabled = false; port.Enabled = false; inforVer.Enabled = false; ServerCon();
            }
            if (csName.pl[38, 1] == "手动更新") { toolStripComboBox1.Text = "手动更新"; csName.webSatelliteData = false; }
            else { toolStripComboBox1.Text = "网络获取"; csName.webSatelliteData = true; }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //窗口自适应
            windowAdaptation.x = this.Width;
            windowAdaptation.y = this.Height;
            windowAdaptation.setTag(this);
            WriteInfo("系统开机！");
            this.chart1.Series[1].Points.AddXY(csName.fWjD, csName.fYjD);//天线位置
            //预置位置读取
            using (StreamReader dry = File.OpenText(Environment.CurrentDirectory + "\\text\\txYzwz.txt"))
            {
                for (int i = 0; ; i++)
                {
                    if (dry.EndOfStream) { break; }
                    csName.yzWz[i] = dry.ReadLine();
                    cbYzwz.Items.Add(csName.yzWz[i].Substring(0, csName.yzWz[i].IndexOf(',', 0)));
                }
                cbYzwz.SelectedIndex = 0;
            }
            #region 星历信息记忆信息读取选择
            if (csName.Mode == 1)
            {
                toolStripMenuItem3.Enabled = false;
                //处理方式选择
                if (csName.pl[1, 1] == "单星任务")
                {
                    单星任务ToolStripMenuItem.Enabled = true;
                    更改信息ToolStripMenuItem.Enabled = true;
                    数据更新ToolStripMenuItem.Enabled = true;
                    多星任务ToolStripMenuItem.Enabled = false;
                    星历更新ToolStripMenuItem.Enabled = false;
                    数据ToolStripMenuItem.Enabled = false;
                    tcwxNames.Items.Add(csName.pl[2, 1]);
                    tcwxNames.Text = label54.Text=lblWx.Text = csName.pl[2, 1];//卫星名
                }
                else
                {
                    单星任务ToolStripMenuItem.Enabled = false;
                    更改信息ToolStripMenuItem.Enabled = false;
                    数据更新ToolStripMenuItem.Enabled = false;
                    多星任务ToolStripMenuItem.Enabled = true;
                    星历更新ToolStripMenuItem.Enabled = true;
                    数据ToolStripMenuItem.Enabled = true;
                    for (int i = 0; i < 9999; i++)
                    {
                        if (csName.sxlm[i] == null) { break; }
                        tcwxNames.Items.Add(csName.sxlm[i]);
                    }
                    this.tcwxNames.Text = label54.Text = lblWx.Text = csName.pl[2, 1];
                    csName.wxSy = tcwxNames.Items.IndexOf(tcwxNames.Text);
                }
                //天线状态
                chartZTGraphThread = new Thread(chartZT) { IsBackground = true };
                chartZTGraphThread.Start();
                //卫星跟踪信息
                wxNewsGraphThread = new Thread(wxNews) { IsBackground = true };
                wxNewsGraphThread.Start();
            }
            else if (csName.Mode == 2)
            {
                //toolStripMenuItem1.Visible = false;
                    toolStripMenuItem2.Visible=tmYC.Visible = 数据跟踪记录间隔ToolStripMenuItem.Visible = 跟踪数据存储位置ToolStripMenuItem.Visible = tmGX.Visible = 天线姿态解算ToolStripMenuItem.Visible = 卫星接收轨道时间段ToolStripMenuItem.Visible = 两行根数辅助查看ToolStripMenuItem.Visible =
                    跟踪模式tsmGzms.Visible = 时间补偿toolStripMenuItem10.Visible = 时间补偿toolStripMenuItem2.Visible = false;
                gzMs.Enabled = btnTxsc.Visible = btnSjjl.Visible = false;
                tabControl1.TabPages.Remove(tabPage2);
                tabControl1.TabPages.Remove(tabPage3);
                tbBX.TabPages.Remove(tabPage5);
                lbltQc.Text = "经度";
                btnZdzx.Visible = 星历处理方式ToolStripMenuItem.Visible = 更新星历ToolStripMenuItem.Visible = 记跟数据更新toolStripMenuItem1.Visible = 卫星选择toolStripMenuItem12.Visible = false;
                toolStripComboBox2.Items.AddRange(csName.satelliteName);
                toolStripComboBox2.Text = label54.Text = lblWx.Text = csName.pl[46, 1];
                tongbu = true;
                lbltKssj.Text = "当地经度"; lbltJssj.Text = "当地纬度";
                btnFwml_Click(null, null);
                btnFyml_Click(null, null);
            }
            #endregion
            if (csName.pl[39, 1] == "AZEL")
            {
                lbltFw.Text = lbltFw2.Text = "方位轴";
                lbltFy.Text = lbltFy2.Text = "俯仰轴";
                btnFwkj.Text = "方位开机";
                btnFykj.Text = "俯仰开机";
                btnFwdj.Text = "方位待机";
                btnFydj.Text = "俯仰待机";
                btnFwsm.Text = "方位扫描";
                btnFysm.Text = "俯仰扫描";
                btnFwsk.Text = "方位手控";
                btnFysk.Text = "俯仰手控";
                lbltFwsd.Text = btnFwss.Text = "方位速度";
                lbltFysd.Text = btnFyss.Text = "俯仰速度";
                lbltFwml.Text = btnFwml.Text = "方位命令";
                lbltFyml.Text = btnFyml.Text = "俯仰命令";
                btnFwcg.Text = "方位程跟";
                btnFycg.Text = "俯仰程跟";
                btnFwzg.Text = "方位数引";
                btnFyzg.Text = "俯仰数引";
                label3.Text = "方位/俯仰控制";
                label12.Text = "方位速度";
                label13.Text = "俯仰速度";
                lbltFw3.Text = label15.Text = "方位";
                label9.Text = label17.Text = "俯仰";
                lbltFwsdzl.Text = "方位速度指令";
                lbltFysdzl.Text = "俯仰速度指令";
                X轴扫描范围ToolStripMenuItem.Text = "方位扫描范围(º)";
                X轴扫描速度ToolStripMenuItem.Text = "方位扫描速度(º/S)";
                Y轴扫描范围ToolStripMenuItem.Text = "俯仰扫描范围(º)";
                Y轴扫描速度ToolStripMenuItem.Text = "俯仰扫描速度(º/S)";
                X轴命令速度ToolStripMenuItem.Text = "方位命令速度";
                Y轴命令速度ToolStripMenuItem.Text = "俯仰命令速度";
                X轴顺限ToolStripMenuItem.Text = "方位顺限(º)";
                X轴逆限ToolStripMenuItem.Text = "方位逆限(º)";
                Y轴上限ToolStripMenuItem.Text = "俯仰顺限(º)";
                Y轴下限ToolStripMenuItem.Text = "俯仰逆限(º)";
                X轴角修正值ToolStripMenuItem.Text = "方位角修正值";
                Y轴角修正值ToolStripMenuItem.Text = "俯仰角修正值";
                //Main.chartArea1.chartArea1.AxisX.Interval = 10D;
                // chartArea1.AxisX.LabelAutoFitStyle = System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont;
                // chartArea1.AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
                // chartArea1.AxisX.Maximum = 90D;
                // chartArea1.AxisX.Minimum = -90D;
                // chartArea1.AxisY.Interval = 15D;
                // chartArea1.AxisY.LabelAutoFitStyle = System.Windows.Forms.DataVisualization.Charting.LabelAutoFitStyles.DecreaseFont;
                // chartArea1.AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dash;
                // chartArea1.AxisY.Maximum = 90D;
                // chartArea1.AxisY.Minimum = -90D;
            }
            //转台连接
            string[] adder = new string[] { csName.pl[49, 1].Split(',')[0], csName.pl[50, 1].Split(',')[0], csName.pl[51, 1].Split(',')[0] };
            int[] bote = new int[] { Convert.ToInt32(csName.pl[49, 1].Split(',')[1]), Convert.ToInt32(csName.pl[50, 1].Split(',')[1]), Convert.ToInt32(csName.pl[51, 1].Split(',')[1]) };
            int[] bote2 = new int[] { Convert.ToInt32(csName.pl[49, 1].Split(',')[2]), Convert.ToInt32(csName.pl[50, 1].Split(',')[2]), Convert.ToInt32(csName.pl[51, 1].Split(',')[2]) };
            char[] bote3 = new char[] { Convert.ToChar(csName.pl[49, 1].Split(',')[3]), Convert.ToChar(csName.pl[50, 1].Split(',')[3]), Convert.ToChar(csName.pl[51, 1].Split(',')[3]) };
            int[] bote4 = new int[] { Convert.ToInt32(csName.pl[49, 1].Split(',')[4]), Convert.ToInt32(csName.pl[50, 1].Split(',')[4]), Convert.ToInt32(csName.pl[51, 1].Split(',')[4]) };
            AxisControl.Connect(adder, bote, bote2, bote3, bote4);
            AxisControl.setCorrected(0, Convert.ToDouble(csName.pl[8, 1]));
            AxisControl.setCorrected(1, Convert.ToDouble(csName.pl[9, 1]));
            AxisControl.setCorrected(2, Convert.ToDouble(csName.pl[43, 1]));
            AxisControl.setRLimit(0, Convert.ToDouble(csName.pl[19, 1]), Convert.ToDouble(csName.pl[20, 1]));
            AxisControl.setRLimit(1, Convert.ToDouble(csName.pl[21, 1]), Convert.ToDouble(csName.pl[22, 1]));
            AxisControl.setRLimit(2, Convert.ToDouble(csName.pl[41, 1]), Convert.ToDouble(csName.pl[42, 1]));
            btnFwkj_Click(null, null);
            btnFykj_Click(null, null);
            BtnQxkj_Click(null, null);
            int ci= (Convert.ToInt32(csName.pl[53, 1]) +41)/3;
            if (ci > 0) { Environment.Exit(0); ; return; }
            ci = (ci+1) * 3 + -41;
            string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
            if (File.Exists(strFilePath))
            {
                string strContent = File.ReadAllText(strFilePath);
                strContent = Regex.Replace(strContent, "运行状态:" + csName.pl[53, 1], "运行状态:" + ci.ToString());
                File.WriteAllText(strFilePath, strContent);
            }
        }
        private void Form1_Resize(object sender, EventArgs e) //窗体重绘
        {
            float newx = (this.Width) / windowAdaptation.x;
            float newy = (this.Height) / windowAdaptation.y;
            windowAdaptation.setControls(newx, newy, this);
        }
        #endregion

        #region 系统退出
        private void btnXttc_Click(object sender, EventArgs e) { this.Close(); }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("是否确认退出系统?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            if (result == DialogResult.Yes)
            {
                WriteInfo("系统关机！");
                if (!LogSave(rzLj.Text))//保存失败
                {
                    DialogResult result1 = MessageBox.Show("未找到日志存储路径,是否继续退出?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                    if (result1 == DialogResult.Yes)
                    {
                        AxisControl.setMove(0, 0);//X轴停止
                        AxisControl.setMove(1, 0);//Y轴停止
                        AxisControl.setMove(2, 0);
                        btnFwdj_Click(null, null);
                        btnFydj_Click(null, null);
                        BtnQxdj_Click(null, null);
                        AxisControl.DisConnect();
                        Environment.Exit(0);
                    }
                    else { try { WriteInfo("系统关机失败，未找到日志存储路径！"); e.Cancel = true; } catch { } }
                }
                else
                {
                    AxisControl.setMove(0, 0);//X轴停止
                    AxisControl.setMove(1, 0);//Y轴停止
                    AxisControl.setMove(2, 0);
                    AxisControl.DisConnect();
                    Environment.Exit(0);
                }
            }
            else
            {
                try { e.Cancel = true; } catch { }
            }
        }
        #endregion

        #region 数据读取处理线程

        //天线状态
        bool YesGet = false;//是否绘制运行轨迹
        Thread chartZTGraphThread;
        public void chartZT()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    //天线状态
                    DateTime timeksPd = DateTime.Now;
                    DateTime timeksPd2 = timeksPd;
                    if (csName.pl[23, 1] == "程序跟踪")
                    {
                        if (csName.mksTime != "")
                        {
                            timeksPd = Convert.ToDateTime(csName.mksTime.Substring(0, 19));
                            timeksPd2 = Convert.ToDateTime(csName.mjsTime.Substring(0, 19));
                        }
                    }
                    else
                    {
                        timeksPd = Convert.ToDateTime(csName.jgNew[1].Substring(5));
                        timeksPd2 = Convert.ToDateTime(csName.jgNew[2].Substring(5));
                    }
                    if (DateTime.Now > timeksPd && DateTime.Now < timeksPd2)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            if (Convert.ToDouble(csName.fYYsjD) <= 0.001 && Convert.ToDouble(csName.fYjyjD) <= 0.001)
                            {
                                YesGet = false;
                                Thread.Sleep(1000);
                                this.Invoke((EventHandler)(delegate
                                {
                                    this.chart2.Series[1].Points.Clear();//清除拟合跟踪曲线
                                    this.chart1.Series[0].Points.Clear();
                                }
                        ));
                                break;
                            }
                            YesGet = true;
                            j = 0;
                            Thread.Sleep(10);
                        }
                    }
                }
                catch { }
                Thread.Sleep(100);
                i = 0;
            }
        }
        //拟合曲线
        bool wcHz = false;
        ArrayList nhFw = new ArrayList(1); //拟合曲线数组
        ArrayList nhFy = new ArrayList(1); //拟合曲线数组
        DateTime sfB = new DateTime();
        DateTime ksS = new DateTime();
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (csName.pl[23, 1] == "程序跟踪" && csName.mksTime != "" && csName.mjsTime != "")
            {
                ksS = Convert.ToDateTime(csName.mksTime.Substring(0, 19));
            }
            else if (csName.pl[23, 1] == "数据引导")
            {
                ksS = Convert.ToDateTime(csName.jgNew[1].Substring(5));
            }
            if (tabControl1.SelectedTab.Name == "tabPage3" && chartNHGraphThread == null && sfB != ksS)
            {
                sfB = ksS;
                this.chart2.Series[0].Points.Clear();
                nhFw.Clear();
                nhFy.Clear();
                nhFw.Add(0);
                nhFy.Add(0);
                chartNHGraphThread = new Thread(chartNH);
                chartNHGraphThread.Start();
            }
        }
        Thread chartNHGraphThread;
        public void chartNH()
        {
            try
            {
                if (csName.pl[23, 1] == "程序跟踪")
                {
                    DateTime ksTime = DateTime.Now;
                    DateTime jsTime = DateTime.Now;
                    string str0 = "";
                    string str1 = "";
                    string str2 = "";
                    int z = csName.wxSy;
                    if (csName.pl[1, 1] == "单星任务")
                    {
                        str0 = csName.dXl[0];//星历1行
                        str1 = csName.dXl[1];//星历2行
                        str2 = csName.dXl[2];//星历3行
                        ksTime = Convert.ToDateTime((csName.dtimfNew[csName.dfk].Substring(5, 19)).ToString()).AddMilliseconds(Convert.ToInt32(csName.dtimfNew[csName.dfk].Substring(25, 3)));
                        jsTime = Convert.ToDateTime((csName.dtimfNew[csName.dfk + 1].Substring(5, 19)).ToString()).AddMilliseconds(Convert.ToInt32(csName.dtimfNew[csName.dfk + 1].Substring(25, 3)));
                    }
                    else
                    {
                        str0 = csName.sXl[3 * z];//星历1行
                        str1 = csName.sXl[3 * z + 1];//星历2行
                        str2 = csName.sXl[3 * z + 2];//星历3行
                        ksTime = Convert.ToDateTime((csName.stimfNew[csName.sfk].Substring(5, 19)).ToString()).AddMilliseconds(Convert.ToInt32(csName.stimfNew[csName.sfk].Substring(25, 3)));
                        jsTime = Convert.ToDateTime((csName.stimfNew[csName.sfk + 1].Substring(5, 19)).ToString()).AddMilliseconds(Convert.ToInt32(csName.stimfNew[csName.sfk + 1].Substring(25, 3)));
                    }
                    string strJWH = csName.pl[0, 1];
                    strArray = strJWH.Split(',');
                    double dbw = Convert.ToDouble(strArray[0]);
                    double dbj = Convert.ToDouble(strArray[2]);
                    double dbh = Convert.ToDouble(strArray[4]);
                    ArrayList str = ayxjClass.timeAngle(str0, str1, str2, dbw, dbj, dbh, ksTime, jsTime, 1000, 8);
                    for (int k = 0; ; k++)
                    {
                        string[] data = new string[2];
                        try
                        { data = str[k].ToString().Split(','); }
                        catch {; break; }
                        nhFw.Add(Convert.ToDouble(data[1]));
                        nhFy.Add(Convert.ToDouble(data[2]));
                    }
                    wcHz = true;
                }
                else//数据引导
                {
                    for (int k = 0; k < 999999999; k++)
                    {
                        string[] str = new string[2];
                        try
                        { str = csName.jgNew[k + 6].Split(','); }
                        catch {; break; }
                        if ((Math.Abs((Convert.ToDouble(nhFw[nhFw.Count - 1])) - Convert.ToDouble(str[1]))) >= 1 || (Math.Abs((Convert.ToDouble(nhFy[nhFy.Count - 1])) - Convert.ToDouble(str[2])) >= 1))
                        {
                            nhFw.Add(Convert.ToDouble(str[1]));
                            nhFy.Add(Convert.ToDouble(str[2]));
                        }
                    }
                    wcHz = true;
                }
                chartNHGraphThread.DisableComObjectEagerCleanup();
                chartNHGraphThread = null;
            }
            catch
            {

            }
        }




        ////卫星跟踪数据
        string[] strArray = new string[5];//当地经纬信息
        double[] su = new double[2];
        DateTime[] datime = new DateTime[1000];
        int kg = 1;
        int jG = 6;//记忆跟踪循环
        bool ysColor = false;//预设角度颜色
        bool jyColor = false;//记忆角度颜色
        bool sjLow = true;//数据引导过期提醒
        double[] ydSjFw = new double[2];
        double[] ydSjFy = new double[2];
        Thread wxNewsGraphThread;
        public void wxNews()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    if (csName.pl[23, 1] == "程序跟踪")
                    {
                        DateTime nowTime = DateTime.Now;//获取当前时间                
                        int nownian = Convert.ToInt32((nowTime.Year).ToString().Substring(2));//当前年份后两位
                        double nowdays = Convert.ToDouble((nowTime.DayOfYear + nowTime.Hour / 24.000000000 + nowTime.Minute / 1440.000000000 + nowTime.Second / 86400.000000000 + nowTime.Millisecond / 86400000.000000000));//本年至今天数
                        double shiC = 0.00000000;//星历时刻后至今分钟数
                        if (kg == 1)
                        {
                            //星历信息
                            if (csName.pl[1, 1] == "单星任务")
                            {
                                int znian = Convert.ToInt32(csName.dXl[1].Substring(18, 2));//年份后两位
                                double zdays = Convert.ToDouble(csName.dXl[1].Substring(20, 12));//轨道报天数
                                shiC = ayxjClass.GetMinute(nownian, znian, nowdays, zdays, nowTime.Year) - 480;//计算星历时刻后至今分钟数
                                string str1 = csName.dXl[0];//星历1行
                                string str2 = csName.dXl[1];//星历2行
                                string str3 = csName.dXl[2];//星历3行
                                string strJWH = csName.pl[0, 1];//经纬度解算
                                strArray = strJWH.Split(',');
                                double dbWD = Convert.ToDouble(strArray[0]);
                                double dbJD = Convert.ToDouble(strArray[2]);
                                double dbGD = Convert.ToDouble(strArray[4]);
                                //
                                csName.mWxName = str1;//卫星名
                                csName.mQc = ((Convert.ToDouble(csName.dXl[2].Substring(63, 5))
                                    + ((Convert.ToDouble(csName.dXl[2].Substring(52, 11)) / 1440.00000000) * shiC))).ToString("00.00000");//当前圈次
                                //更换卫星跟踪时间段
                                if (csName.dtimfNew != null)
                                {
                                    if (csName.dtimfNew[csName.dfk + 1] == null && csName.mksTime != "")
                                    {
                                        csName.mksTime = "";
                                        csName.mjsTime = "";
                                        csName.fWYsjD = 0;
                                        csName.fYYsjD = 0;
                                        //单星历更新
                                        oneGraphThread = new Thread(oneGraph);
                                        oneGraphThread.Start();
                                        kg = 0;
                                    }
                                    else
                                    {
                                        if (csName.dtimfNew[csName.dfk].Substring(0, 4) == "结束时间")
                                        { csName.dfk++; }
                                        else
                                        {
                                            csName.mksTime = (csName.dtimfNew[csName.dfk].Substring(5, 23)).ToString();
                                            csName.mjsTime = (csName.dtimfNew[csName.dfk + 1].Substring(5, 23)).ToString();
                                            if (ysColor == false)
                                            {
                                                csName.fWYsjD = Convert.ToDouble(csName.dtimfNew[csName.dfk].Substring(32, 8));
                                                csName.tfWjD[1] = csName.tfWjD[0] = csName.fWYsjD;
                                                csName.fYYsjD = Convert.ToDouble(csName.dtimfNew[csName.dfk].Substring(48, 7));
                                                csName.tfYjD[1] = csName.tfYjD[0] = csName.fYYsjD;
                                                ysColor = false;// 预设角度字色
                                            }
                                            if (nowTime.AddSeconds(-5) > Convert.ToDateTime(csName.dtimfNew[csName.dfk + 1].Substring(5, 19)))
                                            {
                                                csName.dfk = csName.dfk + 2;
                                                ysColor = false;
                                            }
                                        }
                                    }
                                }
                                //跟踪时间段内解算实时角度
                                if (csName.dtimfNew != null && Convert.ToDateTime(csName.dtimfNew[csName.dfk].Substring(5, 19)).AddMilliseconds(Convert.ToInt32(csName.dtimfNew[csName.dfk].Substring(25, 3))) < nowTime &&
                                    Convert.ToDateTime(csName.dtimfNew[csName.dfk + 1].Substring(5, 19)).AddMilliseconds(Convert.ToInt32(csName.dtimfNew[csName.dfk + 1].Substring(25, 3))) > nowTime)
                                {
                                    ysColor = true;// 预设角度字色
                                    csName.fWYsjD = csName.tfWjD[1] = csName.tfWjD[0];//显示X轴
                                    csName.fYYsjD = csName.tfYjD[1] = csName.tfYjD[0];//显示Y轴
                                    double[] fwfy = new double[2];
                                    double Bctime = (Convert.ToDouble(csName.pl[17, 1])) / 60000;//时间补偿
                                    ayxjClass.degInfo hu = new ayxjClass.degInfo();
                                    ayxjClass.GetPosition(out fwfy[0], out fwfy[1], out hu.degX, out hu.degY, str1, str2, str3, dbWD, dbJD, dbGD, shiC + Bctime);
                                    csName.tfWjD[0] = fwfy[0];//显示X轴
                                    csName.tfYjD[0] = fwfy[1];//显示Y轴
                                    ysColor = true;// 预设角度字色
                                }
                            }
                            else//多星
                            {
                                int z = csName.wxSy;//卫星索引
                                int znian = Convert.ToInt32(csName.sXl[3 * z + 1].Substring(18, 2));//年份后两位
                                double zdays = Convert.ToDouble(csName.sXl[3 * z + 1].Substring(20, 12));//轨道报天数
                                shiC = ayxjClass.GetMinute(nownian, znian, nowdays, zdays, nowTime.Year) - 480;//计算至星历时刻后至今分钟数
                                string str1 = csName.sXl[3 * z];//星历1行
                                string str2 = csName.sXl[3 * z + 1];//星历2行
                                string str3 = csName.sXl[3 * z + 2];//星历3行
                                //
                                string strJWH = csName.pl[0, 1];
                                strArray = strJWH.Split(',');
                                double dbWD = Convert.ToDouble(strArray[0]);
                                double dbJD = Convert.ToDouble(strArray[2]);
                                double dbGD = Convert.ToDouble(strArray[4]);
                                //
                                csName.mWxName = str1;//卫星名
                                csName.mQc = ((Convert.ToDouble(str3.Substring(63, 5)) + ((Convert.ToDouble(str3.Substring(52, 11)) / 1440.00000000) * shiC))).ToString("00.00000");//当前圈次
                                //
                                if (csName.stimfNew != null)
                                {
                                    if (csName.stimfNew[csName.sfk + 1] == null && csName.mksTime != "")
                                    {
                                        csName.mksTime = "";
                                        csName.mjsTime = "";
                                        csName.fWYsjD = 0;
                                        csName.fYYsjD = 0;
                                        //多星历更新
                                        mostGraphThread = new Thread(mostGraph);
                                        mostGraphThread.Start();
                                        kg = 0;
                                    }
                                    else
                                    {
                                        if (csName.stimfNew[csName.sfk].Substring(0, 4) == "结束时间")
                                        { csName.sfk++; }
                                        else
                                        {
                                            csName.mksTime = (csName.stimfNew[csName.sfk].Substring(5, 23)).ToString();
                                            csName.mjsTime = (csName.stimfNew[csName.sfk + 1].Substring(5, 23)).ToString();
                                            if (ysColor == false)
                                            {
                                                csName.fWYsjD = Convert.ToDouble(csName.stimfNew[csName.sfk].Substring(32, 8));
                                                csName.tfWjD[1] = csName.tfWjD[0] = csName.fWYsjD;
                                                csName.fYYsjD = Convert.ToDouble(csName.stimfNew[csName.sfk].Substring(48, 7));
                                                csName.tfYjD[1] = csName.tfYjD[0] = csName.fYYsjD;
                                                ysColor = false;
                                            }
                                            if (nowTime.AddSeconds(-5) > Convert.ToDateTime(csName.stimfNew[csName.sfk + 1].Substring(5, 19)))
                                            {
                                                csName.sfk = csName.sfk + 2;
                                                ysColor = false;
                                            }
                                        }
                                    }
                                }
                                //
                                if (csName.stimfNew != null && Convert.ToDateTime(csName.stimfNew[csName.sfk].Substring(5, 19)).AddMilliseconds(Convert.ToInt32(csName.stimfNew[csName.sfk].Substring(25, 3))) < nowTime
                                    && Convert.ToDateTime(csName.stimfNew[csName.sfk + 1].Substring(5, 19)).AddMilliseconds(Convert.ToInt32(csName.stimfNew[csName.sfk + 1].Substring(25, 3))) > nowTime)
                                {
                                    ysColor = true;// 预设角度字色
                                    csName.fWYsjD = csName.tfWjD[1] = csName.tfWjD[0];//显示X轴
                                    csName.fYYsjD = csName.tfYjD[1] = csName.tfYjD[0];//显示Y轴
                                    double[] fwfy = new double[2];
                                    double Bctime = (Convert.ToDouble(csName.pl[17, 1])) / 60000;//时间补偿
                                    ayxjClass.degInfo hu = new ayxjClass.degInfo();
                                    ayxjClass.GetPosition(out fwfy[0], out fwfy[1], out hu.degX, out hu.degY, str1, str2, str3, dbWD, dbJD, dbGD, shiC + Bctime);
                                    csName.tfWjD[0] = fwfy[0];//显示X轴
                                    csName.tfYjD[0] = fwfy[1];//显示Y轴
                                    ysColor = true;// 预设角度字色

                                }
                            }
                        }
                    }
                    else//数据引导
                    {
                        if (Convert.ToDateTime(csName.jgNew[2].Substring(5)) >= DateTime.Now)
                        {
                            sjLow = true;
                            csName.mWxName = csName.jgNew[0].Substring(4);
                            csName.mQc = "None";
                            DateTime nowTime = DateTime.Now;//获取当前时间  
                            if (Convert.ToDateTime(csName.jgNew[1].Substring(5)) < nowTime)
                            {
                                double Bctime = Convert.ToDouble(csName.pl[18, 1]);//时间补偿
                                if (Convert.ToDateTime(csName.jgNew[1].Substring(5)).AddMilliseconds((jG - 5) * Convert.ToInt32(csName.jgNew[3].Substring(9))) <= nowTime.AddMilliseconds(Bctime))
                                {
                                    jyColor = true;
                                    string[] str = csName.jgNew[jG].Split(',');
                                    csName.fWjyjD = ydSjFw[1] = Convert.ToDouble(str[1]);
                                    csName.fYjyjD = ydSjFy[1] = Convert.ToDouble(str[2]);
                                    str = csName.jgNew[jG + 5].Split(',');
                                    ydSjFw[0] = Convert.ToDouble(str[1]);
                                    ydSjFy[0] = Convert.ToDouble(str[2]);
                                    jG++;
                                    if (Convert.ToInt32(csName.jgNew[3].Substring(9)) == 10) { jG += 6; }
                                }
                            }
                            else
                            {
                                jyColor = false;
                                string[] str = csName.jgNew[6].Split(',');
                                ydSjFw[0] = ydSjFw[1] = csName.fWjyjD = Convert.ToDouble(str[1]);
                                ydSjFy[0] = ydSjFy[1] = csName.fYjyjD = Convert.ToDouble(str[2]);
                                jG = 6;
                            }
                        }
                        else if (sjLow == true)
                        {
                            sjLow = false;
                            jyColor = false;
                            Invoke((EventHandler)(delegate
                            {
                                this.chart2.Series[1].Points.Clear();
                                this.chart1.Series[0].Points.Clear();
                                WriteInfo("引导数据已过期,请更新数据或切换程序跟踪！");
                            }
                ));
                        }
                    }
                }
                catch
                {

                }
                i = 0;
                Thread.Sleep(5);
            }
        }
        #endregion

        #region UI数据显示刷新
        //数据实时显示事件
        double fwVs = 0;//X轴
        double fyVs = 0;//Y轴
        private void timerReadCom_Tick(object sender, EventArgs e)
        {
            try
            {
                if (tabControl1.Focused == true || tbBX.Focused == true) { cee.Focus(); }//焦点获取
                csName.mxtTime = txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");//系统时间
                                                                                                 //实际角度
                csName.fWjD = AxisControl.getPosition(0);
                lblSjfw.Text = lblTxjdfw.Text = csName.fWjD.ToString("00.0000");//X轴实际
                csName.fYjD = AxisControl.getPosition(1);
                lblSjfy.Text = lblTxjdfy.Text = csName.fYjD.ToString("00.0000");//Y轴实际
                csName.PjD = AxisControl.getPosition(2);
                lblTxjdqx.Text = csName.PjD.ToString("00.0000");//P轴实际
                                                                //速度
                sd1.Text = csName.sdssFw = AxisControl.getSpeed(0).ToString("0.000") + "°/s";//实时X轴速度
                sd2.Text = csName.sdssFy = AxisControl.getSpeed(1).ToString("0.000") + "°/s"; ;//实时Y轴速度
                sd3.Text = csName.sdssJh = AxisControl.getSpeed(2).ToString("0.000") + "°/s"; ;//实时极化轴速度
                                                                                               //使能
                if (AxisControl.getEnable(0)) { fw1.BackColor = Color.Lime; } else { fw1.BackColor = Color.DarkGray; }
                if (AxisControl.getEnable(1)) { fy1.BackColor = Color.Lime; } else { fy1.BackColor = Color.DarkGray; }
                if (AxisControl.getEnable(2)) { jh1.BackColor = Color.Lime; } else { jh1.BackColor = Color.DarkGray; }
                //限位
                int Limt = AxisControl.getLimit(0);
                if (Limt == 1) { fw2.BackColor = Color.Red; fw5.BackColor = Color.DarkGray; }
                else if (Limt == 2) { fw2.BackColor = Color.DarkGray; fw5.BackColor = Color.Red; }
                else { fw2.BackColor = Color.DarkGray; fw5.BackColor = Color.DarkGray; }
                Limt = AxisControl.getLimit(1);
                if (Limt == 1) { fy2.BackColor = Color.Red; fy5.BackColor = Color.DarkGray; }
                else if (Limt == 2) { fy2.BackColor = Color.DarkGray; fy5.BackColor = Color.Red; }
                else { fy2.BackColor = Color.DarkGray; fy5.BackColor = Color.DarkGray; }
                Limt = AxisControl.getLimit(2);
                if (Limt == 1) { jh2.BackColor = Color.Red; jh5.BackColor = Color.DarkGray; }
                else if (Limt == 2) { jh2.BackColor = Color.DarkGray; jh5.BackColor = Color.Red; }
                else { jh2.BackColor = Color.DarkGray; jh5.BackColor = Color.DarkGray; }
                //运动状态
                int State = AxisControl.getState(0);
                if (State == 1) { fw3.BackColor = Color.Lime; fw4.BackColor = Color.DarkGray; sd1.BackColor = Color.Lime; }
                else if (State == 2) { fw3.BackColor = Color.DarkGray; fw4.BackColor = Color.Lime; sd1.BackColor = Color.Lime; }
                else { fw3.BackColor = Color.DarkGray; fw4.BackColor = Color.DarkGray; sd1.BackColor = Color.Gainsboro; }
                State = AxisControl.getState(1);
                if (State == 1) { fy3.BackColor = Color.Lime; fy4.BackColor = Color.DarkGray; sd2.BackColor = Color.Lime; }
                else if (State == 2) { fy3.BackColor = Color.DarkGray; fy4.BackColor = Color.Lime; sd2.BackColor = Color.Lime; }
                else { fy3.BackColor = Color.DarkGray; fy4.BackColor = Color.DarkGray; sd2.BackColor = Color.Gainsboro; }
                State = AxisControl.getState(2);
                if (State == 1) { jh3.BackColor = Color.Lime; jh4.BackColor = Color.DarkGray; sd3.BackColor = Color.Lime; }
                else if (State == 2) { jh3.BackColor = Color.DarkGray; jh4.BackColor = Color.Lime; sd3.BackColor = Color.Lime; }
                else { jh3.BackColor = Color.DarkGray; jh4.BackColor = Color.DarkGray; sd3.BackColor = Color.Gainsboro; }
                //告警
                string err = AxisControl.getErrStr(0);
                if (err == "NONE") { fw7.Text = "告警"; fw6.BackColor = Color.DarkGray; }
                else { fw7.Text = err; fw6.BackColor = Color.Red; }
                err = AxisControl.getErrStr(1);
                if (err == "NONE") { fy7.Text = "告警"; fy6.BackColor = Color.DarkGray; }
                else { fy7.Text = err; fy6.BackColor = Color.Red; }
                err = AxisControl.getErrStr(2);
                if (err == "NONE") { jh7.Text = "告警"; jh6.BackColor = Color.DarkGray; }
                else { jh7.Text = err; jh6.BackColor = Color.Red; }

                //
                if (csName.Mode == 1)
                {
                    label28.Text = csName.fwQs.ToString();//X轴圈次
                    label54.Text = lblWx.Text = csName.mWxName;//卫星名
                    lblQc.Text = label48.Text = csName.mQc;//卫星圈次
                    lblYsfw.Text = csName.fWYsjD.ToString("00.0000");//X轴预设
                    lblYsfy.Text = csName.fYYsjD.ToString("00.0000");//Y轴预设
                    lblTxmlqx.Text = csName.YUAngle.ToString("00.0000");//P轴预制
                    lblTxmlfw.Text = csName.fWjyjD.ToString("00.0000");//X轴引导角度
                    lblTxmlfy.Text = csName.fYjyjD.ToString("00.0000");//Y轴引导角度
                    if (ysColor == false) { lblYsfw.ForeColor = Color.Yellow; lblYsfy.ForeColor = Color.Yellow; }//预设角度颜色
                    else { lblYsfw.ForeColor = Color.Lime; lblYsfy.ForeColor = Color.Lime; }
                    if (jyColor == false) { lblTxmlfw.ForeColor = Color.Yellow; lblTxmlfy.ForeColor = Color.Yellow; }//记忆角度颜色
                    else { lblTxmlfw.ForeColor = Color.Lime; lblTxmlfy.ForeColor = Color.Lime; }
                    try
                    {
                        if (csName.pl[23, 1] == "程序跟踪")
                        {
                            csName.ksTime = csName.mksTime.Substring(11, 12);
                            csName.jsTime = csName.mjsTime.Substring(11, 12);
                        }
                        else
                        {
                            csName.ksTime = Convert.ToDateTime(csName.jgNew[1].Substring(15)).ToString("HH:mm:ss:fff");
                            csName.jsTime = Convert.ToDateTime(csName.jgNew[2].Substring(15)).ToString("HH:mm:ss:fff");
                        }
                    }
                    catch
                    {
                        csName.ksTime = ""; csName.jsTime = "";
                    }
                    lblKssj.Text = csName.ksTime;
                    lblJssj.Text = csName.jsTime;
                    #region //曲线绘制
                    if (wcHz == true)
                    {
                        wcHz = false;
                        for (int i = 1; i < nhFw.Count; i++)
                        {
                            this.chart2.Series[0].Points.AddXY(nhFw[i], nhFy[i]);//拟合曲线
                        }
                    }
                    if (YesGet == true)
                    {
                        if (Math.Abs(fwVs - csName.fWjD) >= 0.5 || Math.Abs(fyVs - csName.fYjD) >= 0.5)
                        {
                            fwVs = csName.fWjD;
                            fyVs = csName.fYjD;
                            this.chart2.Series[1].Points.AddXY(csName.fWjD, csName.fYjD);
                            this.chart1.Series[0].Points.AddXY(csName.fWjD, csName.fYjD);
                        }
                    }
                    #endregion
                }
                else if (csName.Mode == 2)
                {
                    if (tongbu)
                    {
                        tongbu = false;
                        string[] wzXx = csName.pl[0, 1].Split(',');//天线位置
                        ayxjClass.CalculateSatellite(Convert.ToDouble(wzXx[2]), Convert.ToDouble(wzXx[0]), csName.satelliteJd[toolStripComboBox2.SelectedIndex], out double a, out double b, out double c);
                        lblQc.Text =label48.Text= csName.satelliteJd[toolStripComboBox2.SelectedIndex].ToString() + " E";
                        lblYsfw.Text = Math.Round(a, 4).ToString();
                        lblYsfy.Text = Math.Round(b, 4).ToString();
                        lblTxmlqx.Text = Math.Round(c, 4).ToString();
                        label53.Text = csName.satelliteJd1[toolStripComboBox2.SelectedIndex];
                        label47.Text = csName.satelliteJd2[toolStripComboBox2.SelectedIndex]+ "MHz";
                        label46.Text = csName.satelliteJd3[toolStripComboBox2.SelectedIndex] + "MHz";

                    }
                    lblKssj.Text = Convert.ToSingle(csName.pl[0, 1].Split(',')[2]).ToString("0.000") + " E";
                    lblJssj.Text = Convert.ToSingle(csName.pl[0, 1].Split(',')[0]).ToString("0.000") + " N";
                    label45.Text= Convert.ToSingle(csName.pl[0, 1].Split(',')[4]).ToString("0.000") + " km";
                    if (Math.Abs(Convert.ToDouble(lblSjfw.Text) - Convert.ToDouble(lblYsfw.Text)) <= xunxingjingdu) { lblYsfw.ForeColor = Color.Lime; }
                    else { lblYsfw.ForeColor = Color.Yellow; }
                    if (Math.Abs(Convert.ToDouble(lblSjfy.Text) - Convert.ToDouble(lblYsfy.Text)) <= xunxingjingdu) { lblYsfy.ForeColor = Color.Lime; }
                    else { lblYsfy.ForeColor = Color.Yellow; }
                    if (Math.Abs(Convert.ToDouble(lblTxjdqx.Text) - Convert.ToDouble(lblTxmlqx.Text)) <= xunxingjingdu) { lblTxmlqx.ForeColor = Color.Lime; }
                    else { lblTxmlqx.ForeColor = Color.Yellow; }

                }
                #region //门限值
                lblTd.Text = "频率：" + agc.GetFreq().ToString("0.000") + "MHz";
                csName.AGC = agc.GetAGC();
                lblX_AGC.Text = csName.AGC.ToString("0.000");
                if (csName.AGC > Convert.ToDouble(csName.pl[6, 1]) || csName.AGC < Convert.ToDouble(csName.pl[7, 1]))
                {
                    csName.boolAGC = false;
                    lblX_LOCK.Text = "unLOCK";
                    lblX_LOCK.ForeColor = Color.Red;
                    lblX_AGC.ForeColor = Color.Red;
                }
                else
                {
                    csName.boolAGC = true;
                    lblX_LOCK.Text = "LOCK";
                    lblX_LOCK.ForeColor = Color.Lime;
                    lblX_AGC.ForeColor = Color.Lime;
                }
                #endregion
                #region //曲线绘制
                //if (Math.Abs(fwV - csName.fWjD) >= 1.5 || Math.Abs(fyV - csName.fYjD) >= 1.5)
                //{
                //    fwV = csName.fWjD;
                //    fyV = csName.fYjD;
                //    this.chart1.Series[1].Points.Clear();
                //    this.chart1.Series[1].Points.AddXY(csName.fWjD, csName.fYjD);//天线位置
                //}
                #endregion
                if (csName.webSatelliteData)//远端星历更新
                {
                    int today = DateTime.Now.Day;
                    if (NowDate != today)
                    {
                        try { Rotation.GetInstancce().getWebData(); WriteInfo("远端星历更新完成！"); }
                        catch (Exception er) { WriteInfo(er.Message); }
                        NowDate = today;
                    }
                }
            }
            catch (Exception er) { WriteInfo(er.Message); }
        }
        #endregion

        #region 自动执行
        bool zxBool = false;
        private void btnZdzx_Click(object sender, EventArgs e)
        {
            if (btnZdzx.BackColor == Color.Silver)
            {
                gzMs.Enabled = false;
                timeBc.Enabled = false;
                tbclfs.Enabled = false;
                tcwxNames.Enabled = false;
                zxBool = true;
                btnFwsm_Click(null, null);
                btnFysm_Click(null, null);
                zdGraphThread = new Thread(zdZxMl);
                zdGraphThread.Start();
                btnZdzx.BackColor = Color.Lime;
                if (btnSjjl.BackColor == Color.Silver)
                {
                    jlGraphThread = new Thread(sjJlxMl);
                    jlGraphThread.Start();
                    WriteInfo("数据记录功能开启!");
                    btnSjjl.BackColor = Color.Lime;
                }
                btnTxsc.Enabled = false;
                if (gzMs.Text == "程序跟踪")
                {
                    btnFwcg.BackColor = Color.Lime;
                    btnFycg.BackColor = Color.Lime;
                    btnFwzg.BackColor = Color.Silver;
                    btnFyzg.BackColor = Color.Silver;
                }
                else
                {
                    btnFwcg.BackColor = Color.Silver;
                    btnFycg.BackColor = Color.Silver;
                    btnFwzg.BackColor = Color.Lime;
                    btnFyzg.BackColor = Color.Lime;
                }
                WriteInfo("自动执行已启动!");
            }
            else
            {
                gzMs.Enabled = true;
                timeBc.Enabled = true;
                tbclfs.Enabled = true;
                tcwxNames.Enabled = true;
                zxBool = false;
                zdGraphThread.Abort();
                AxisControl.setMove(0, 0);//X轴0速
                AxisControl.setMove(1, 0);//Y轴0速
                btnZdzx.BackColor = Color.Silver;
                btnFwcg.BackColor = Color.Silver;
                btnFycg.BackColor = Color.Silver;
                btnFwzg.BackColor = Color.Silver;
                btnFyzg.BackColor = Color.Silver;
                btnTxsc.Enabled = true;
                WriteInfo("自动执行已取消!");
                wZzxBool = true;
                gzBool = false;
            }
        }
        //自动执行事件
        bool wZzxBool = true;//是否位置执行
        bool gzBool = false;//是否跟踪
        DateTime nowTime = DateTime.Now;
        DateTime nowks = DateTime.Now;
        DateTime nowjs = DateTime.Now;
        DateTime dqjs = DateTime.Now;
        Thread zdGraphThread;
        public void zdZxMl()
        {
            for (int k = 0; k < 10; k++)
            {
                if (zxBool == false) { break; }//自动执行取消
                nowTime = DateTime.Now;
                if (csName.pl[23, 1] == "程序跟踪" && csName.mksTime != "" && csName.mjsTime != "")
                {
                    nowks = Convert.ToDateTime(csName.mksTime.Substring(0, 19)).AddMilliseconds(Convert.ToInt32(csName.mksTime.Substring(20, 3)));
                    nowjs = Convert.ToDateTime(csName.mjsTime.Substring(0, 19)).AddMilliseconds(Convert.ToInt32(csName.mksTime.Substring(20, 3)));
                }
                else if (csName.pl[23, 1] == "数据引导")
                {
                    nowks = Convert.ToDateTime(csName.jgNew[1].Substring(5));
                    nowjs = Convert.ToDateTime(csName.jgNew[2].Substring(5));
                }
                //小于2分钟跟踪起始时间开使能,起始位就位
                if (nowTime >= nowks.AddSeconds(-120) && nowTime < nowjs && wZzxBool == true)
                {
                    ksyxGraphThread = new Thread(ksyx);
                    ksyxGraphThread.Start();
                    wZzxBool = false;
                }
                //小于跟踪起始时间起始位跟踪
                if (nowTime >= nowks && gzBool == true)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("已达跟踪时间,开始跟踪！");
                    }
                     ));
                    dqjs = nowjs;
                    txgzGraphThread = new Thread(txgz);
                    txgzGraphThread.Start();
                    gzBool = false;
                }
                Thread.Sleep(1);
                k = 0;
            }
        }
        //天线开机预置就位
        Thread ksyxGraphThread;
        public void ksyx()
        {
            double fwz = 0;//X轴角
            double fyz = 0;//Y轴角
            this.Invoke((EventHandler)(delegate
            {
                btnFwkj_Click(null, null);
                btnFykj_Click(null, null);
                if (csName.pl[23, 1] == "程序跟踪")
                {
                    fwz = Convert.ToDouble(lblYsfw.Text);
                    fyz = Convert.ToDouble(lblYsfy.Text);
                }
                else
                {
                    fwz = Convert.ToDouble(lblTxmlfw.Text);
                    fyz = Convert.ToDouble(lblTxmlfy.Text);
                }
            }
              ));
            bool fwBol = false;
            bool fyBol = false;
            double fwRxs = Convert.ToDouble(csName.pl[19, 1]);
            double fwRxn = Convert.ToDouble(csName.pl[20, 1]);
            int FH = 1;
            if (fwz >= csName.fWjD)
            {
                if ((fwz - csName.fWjD) <= 180)
                {
                    if (csName.fWySjD + fwz - csName.fWjD >= fwRxs + 1440) { FH = -1; } else { FH = 1; }

                }
                else
                {
                    if ((csName.fWySjD - (360 - fwz + csName.fWjD) <= fwRxn + 1440)) { FH = 1; } else { FH = -1; }
                }
            }
            else
            {
                if ((csName.fWjD - fwz) <= 180)
                {
                    if ((csName.fWySjD - (360 - csName.fWjD + fwz) <= fwRxn + 1440)) { FH = 1; } else { FH = -1; }
                }
                else
                {
                    if (csName.fWySjD + csName.fWjD - fwz >= fwRxs + 1440) { FH = -1; } else { FH = 1; }
                }
            }
            double LRfw = FH * Convert.ToDouble(csName.pl[10, 1]) * csName.fwXs + csName.fwPy;//初速度
            int FHy = 1;
            if (fyz <= csName.fYjD) { FHy = 1; } else { FHy = -1; }
            double LRfy = FHy * Convert.ToDouble(csName.pl[11, 1]) * csName.fyXs + csName.fyPy;//初速度
            for (int i = 0; i < 10; i++)
            {
                if (zxBool == false) { break; }
                //X轴
                if (fwBol == false)
                {
                    if (fwz - Convert.ToDouble(csName.dwJd[2]) <= csName.fWjD && fwz + Convert.ToDouble(csName.dwJd[3]) >= csName.fWjD && fwBol == false)
                    {
                        fwBol = true;
                        AxisControl.setMove(0, 0);//X轴0速
                    }
                    else
                    {
                        double sd = LRfw;
                        if (Math.Abs(csName.fWjD - fwz) <= 6)
                        {
                            sd = Math.Abs(fwz - csName.fWjD) * (LRfw / 6);
                            if (Math.Abs(sd) <= FH * csName.fwXs + csName.fwPy) { sd = FH * csName.fwXs + csName.fwPy; }
                        }
                        AxisControl.setSpeed(0, sd);
                    }
                }
                //X轴限位
                if (AxisControl.getLimit(0) == 2 && FH == -1) { AxisControl.setMove(0, 0); }
                else if (AxisControl.getLimit(0) == 1 && FH == 1) { AxisControl.setMove(0, 0); }
                //Y轴
                if (fyBol == false)
                {
                    if (fyz <= (csName.fYjD + Convert.ToDouble(csName.dwJd[4])) && fyz >= (csName.fYjD - Convert.ToDouble(csName.dwJd[5])))
                    {
                        fyBol = true;
                        AxisControl.setMove(1, 0);
                    }
                    else
                    {
                        double sd = LRfy;
                        if (Math.Abs(fyz - csName.fYjD) <= 5)
                        {
                            sd = Math.Abs(fyz - csName.fYjD) * (LRfy / 5);
                            if (Math.Abs(sd) <= Math.Abs(FHy * csName.fyXs / 2 + csName.fyPy)) { sd = FHy * csName.fyXs / 2 + csName.fyPy; }
                        }
                        AxisControl.setSpeed(1, sd);
                    }
                }
                //Y轴限位
                if (AxisControl.getLimit(1) == 1 && FHy == 1) { AxisControl.setMove(1, 0); }
                else if (AxisControl.getLimit(1) == 2 && FHy == -1) { AxisControl.setMove(1, 0); }
                if (DateTime.Now >= nowks)
                {
                    gzBool = true;//执行跟踪
                    break;
                }
                if (fwBol == true && fyBol == true)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("预置位置就位！");
                    }
            ));
                    gzBool = true;//执行跟踪
                    break;
                }
                Thread.Sleep(20);
                i = 0;
            }
        }
        //天线扫描
        bool smtc = false;//是否跳出扫描
        Thread txsmGraphThread;
        public void txsm()
        {
            double fwman = csName.fWySjD + Convert.ToDouble(csName.pl[14, 1]);//X轴最大值
            if (fwman > Convert.ToDouble(csName.pl[19, 1]) + 1440) { fwman = Convert.ToDouble(csName.pl[19, 1]) + 1440; }
            double fwmin = csName.fWySjD - Convert.ToDouble(csName.pl[14, 1]);//X轴最小值
            if (fwmin < Convert.ToDouble(csName.pl[20, 1]) + 1440) { fwmin = Convert.ToDouble(csName.pl[20, 1]) + 1440; }
            double fyman = csName.fYjD + Convert.ToDouble(csName.pl[15, 1]);//Y轴最大值
            if (fyman > Convert.ToDouble(csName.pl[21, 1])) { fyman = Convert.ToDouble(csName.pl[21, 1]); }
            double fymin = csName.fYjD - Convert.ToDouble(csName.pl[15, 1]);//Y轴最小值
            if (fymin < Convert.ToDouble(csName.pl[22, 1])) { fymin = Convert.ToDouble(csName.pl[22, 1]); }
            AxisControl.setSpeed(0, (Convert.ToDouble(csName.pl[12, 1]) * csName.fwXs + csName.fwPy));//X轴速度
            AxisControl.setSpeed(1, ((-1) * Convert.ToDouble(csName.pl[13, 1]) * csName.fyXs + csName.fyPy));// Y轴速度
            double pd = Convert.ToInt32(csName.pl[28, 1]) * 62;//扫描时长
            this.Invoke((EventHandler)(delegate
            {
                WriteInfo("超出门限范围，开始扫描！");
            }
     ));
            for (int i = 0; i < pd; i++)
            {
                if (zxBool == false || nowTime >= nowjs) { break; }
                if (csName.boolAGC == true)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("扫描成功！");
                        if (csName.pl[23, 1] == "程序跟踪")
                        {
                            double a = csName.fWYsjD - (csName.fWjD);
                            double b = csName.fYYsjD - (csName.fYjD);
                            fwXz.Text = a.ToString();
                            fyXz.Text = b.ToString();
                        }
                        else
                        {
                            double a = csName.fWjyjD - (csName.fWjD);
                            double b = csName.fYjyjD - (csName.fYjD);
                            fwXz.Text = a.ToString();
                            fyXz.Text = b.ToString();
                        }
                        角度修正toolStripMenuItem6_DropDownClosed(null, null);
                    }
                    ));
                    AxisControl.setMove(0, 0);
                    AxisControl.setMove(1, 0);
                    smtc = true;
                    smSb = false;
                    break;
                }
                if (fwSmColor == true)
                {
                    if (csName.fWySjD >= fwman)
                    {
                        AxisControl.setSpeed(0, ((-1) * Convert.ToDouble(csName.pl[12, 1]) * csName.fwXs + csName.fwPy));//X轴速度
                    }
                    if (csName.fWySjD <= fwmin)
                    {
                        AxisControl.setSpeed(0, (Convert.ToDouble(csName.pl[12, 1]) * csName.fwXs + csName.fwPy));//X轴速度
                    }
                }
                if (fySmColor == true)
                {
                    //Y轴速度
                    if (csName.fYjD >= fyman)
                    {
                        AxisControl.setSpeed(1, (Convert.ToDouble(csName.pl[13, 1]) * csName.fyXs + csName.fyPy));//Y轴速度
                    }
                    if (csName.fYjD <= fymin)
                    {
                        AxisControl.setSpeed(1, ((-1) * Convert.ToDouble(csName.pl[13, 1]) * csName.fyXs + csName.fyPy));//Y轴速度
                    }
                }
                Thread.Sleep(20);
            }
            smtc = true;
            smSb = true;
        }
        //天线跟踪  
        double[] wcFwL = new double[3];
        double[] wcFyL = new double[3];
        double fL = 0;
        bool smSb = true;//扫描失败提示
        bool smGet = true;//是否执行扫描
        Thread txgzGraphThread;
        public void txgz()
        {
            if (csName.pl[23, 1] == "程序跟踪")
            {
                for (int i = 0; i < 10; i++)
                {
                    //
                    if (zxBool == false) { break; }
                    //
                    if (DateTime.Now >= dqjs)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("跟踪结束！");
                        }));
                        AxisControl.setMove(0, 0);
                        AxisControl.setMove(1, 0);
                        Thread.Sleep(5000);
                        wZzxBool = true;
                        this.Invoke((EventHandler)(delegate { btnTxsc_Click(null, null); }));
                        break;
                    }
                    //
                    if (csName.boolAGC == true)
                    {
                        //X轴
                        if (csName.fwSk == false)//X轴程跟
                        {
                            double doJd = csName.tfWjD[1];
                            double doJd_1 = Math.Abs(doJd - (csName.fWySjD - 1800));
                            double doJd_2 = Math.Abs(doJd + 360 - (csName.fWySjD - 1800));
                            double doJd_3 = Math.Abs(doJd - 360 - (csName.fWySjD - 1800));
                            double[] array = new double[] { doJd_1, doJd_2, doJd_3 };
                            if (array.Min() == doJd_2) { doJd = doJd + 360; }
                            else if (array.Min() == doJd_3) { doJd = doJd - 360; }
                            double Gs = fL = csName.tfWjD[0] - csName.tfWjD[1];
                            if (Math.Abs(Gs) >= 350) { if (Gs > 0) { Gs = Gs - 360; } else { Gs = 360 - Gs; } }
                            double G = Convert.ToDouble(csName.pl[29, 1].Split(',')[0]);
                            double P = Convert.ToDouble(csName.pl[29, 1].Split(',')[1]);
                            double I = Convert.ToDouble(csName.pl[29, 1].Split(',')[2]);
                            double D = Convert.ToDouble(csName.pl[29, 1].Split(',')[3]);
                            wcFwL[2] = wcFwL[1];
                            wcFwL[1] = wcFwL[0];
                            wcFwL[0] = doJd - (csName.fWySjD - 1800);
                            double Output = P * (wcFwL[0] - wcFwL[1]) + I * wcFwL[0] + D * (wcFwL[0] - wcFwL[1] * 2 + wcFwL[2]);
                            double outFW = (Gs * G + Output * 10) * csName.fwXs + csName.fwPy;
                            AxisControl.setSpeed(0, outFW);//X轴速度
                        }
                        //Y轴
                        if (csName.fySk == false)//Y轴程跟
                        {
                            double Gs = csName.tfYjD[0] - csName.tfYjD[1];
                            double G = Convert.ToDouble(csName.pl[30, 1].Split(',')[0]);
                            double P = Convert.ToDouble(csName.pl[30, 1].Split(',')[1]);
                            double I = Convert.ToDouble(csName.pl[30, 1].Split(',')[2]);
                            double D = Convert.ToDouble(csName.pl[30, 1].Split(',')[3]);
                            wcFyL[2] = wcFyL[1];
                            wcFyL[1] = wcFyL[0];
                            wcFyL[0] = csName.tfYjD[1] - csName.fYjD;
                            double Output = P * (wcFyL[0] - wcFyL[1]) + I * wcFyL[0] + D * (wcFyL[0] - wcFyL[1] * 2 + wcFyL[2]);
                            double outFY = (Gs * G + Output * 10) * csName.fyXs + csName.fyPy;
                            AxisControl.setSpeed(1, -outFY);//Y轴速度
                        }
                    }
                    else//unLOCK
                    {
                        if (fwSmColor == true || fySmColor == true)
                        {
                            for (int j = 0; j < 10; j++)
                            {
                                if (zxBool == false) { break; }
                                if (smGet == true)
                                {
                                    smGet = false;
                                    txsmGraphThread = new Thread(txsm) { IsBackground = true };
                                    txsmGraphThread.Start();
                                }
                                if (smtc == true)
                                {
                                    smtc = false;
                                    smGet = true;
                                    break;
                                }
                                j = 0;
                                Thread.Sleep(5);
                            }
                        }
                        if (smSb == true)
                        {
                            smSb = false;
                            this.Invoke((EventHandler)(delegate
                            {
                                WriteInfo("继续按原定轨道跟踪!");
                            }
                 ));
                        }
                        for (int k = 0; k < 10; k++)
                        {
                            if (zxBool == false) { break; }
                            if (DateTime.Now >= dqjs) { break; }
                            if (csName.boolAGC == false)//扫描失败执行
                            {
                                //X轴
                                if (csName.fwSk == false)//X轴程跟
                                {
                                    double doJd = csName.tfWjD[1];
                                    double doJd_1 = Math.Abs(doJd - (csName.fWySjD - 1800));
                                    double doJd_2 = Math.Abs(doJd + 360 - (csName.fWySjD - 1800));
                                    double doJd_3 = Math.Abs(doJd - 360 - (csName.fWySjD - 1800));
                                    double[] array = new double[] { doJd_1, doJd_2, doJd_3 };
                                    if (array.Min() == doJd_2) { doJd = doJd + 360; }
                                    else if (array.Min() == doJd_3) { doJd = doJd - 360; }
                                    double Gs = fL = csName.tfWjD[0] - csName.tfWjD[1];
                                    if (Math.Abs(Gs) >= 350) { if (Gs > 0) { Gs = Gs - 360; } else { Gs = 360 - Gs; } }
                                    double G = Convert.ToDouble(csName.pl[29, 1].Split(',')[0]);
                                    double P = Convert.ToDouble(csName.pl[29, 1].Split(',')[1]);
                                    double I = Convert.ToDouble(csName.pl[29, 1].Split(',')[2]);
                                    double D = Convert.ToDouble(csName.pl[29, 1].Split(',')[3]);
                                    wcFwL[2] = wcFwL[1];
                                    wcFwL[1] = wcFwL[0];
                                    wcFwL[0] = doJd - (csName.fWySjD - 1800);
                                    double Output = P * (wcFwL[0] - wcFwL[1]) + I * wcFwL[0] + D * (wcFwL[0] - wcFwL[1] * 2 + wcFwL[2]);
                                    double outFW = (Gs * G + Output * 10) * csName.fwXs + csName.fwPy;
                                    AxisControl.setSpeed(0, outFW);//X轴速度
                                }
                                //Y轴
                                if (csName.fySk == false)//Y轴程跟
                                {
                                    double Gs = csName.tfYjD[0] - csName.tfYjD[1];
                                    double G = Convert.ToDouble(csName.pl[30, 1].Split(',')[0]);
                                    double P = Convert.ToDouble(csName.pl[30, 1].Split(',')[1]);
                                    double I = Convert.ToDouble(csName.pl[30, 1].Split(',')[2]);
                                    double D = Convert.ToDouble(csName.pl[30, 1].Split(',')[3]);
                                    wcFyL[2] = wcFyL[1];
                                    wcFyL[1] = wcFyL[0];
                                    wcFyL[0] = csName.tfYjD[1] - csName.fYjD;
                                    double Output = P * (wcFyL[0] - wcFyL[1]) + I * wcFyL[0] + D * (wcFyL[0] - wcFyL[1] * 2 + wcFyL[2]);
                                    double outFY = (Gs * G + Output * 10) * csName.fyXs + csName.fyPy;
                                    AxisControl.setSpeed(1, -outFY);//Y轴速度
                                }
                            }
                            else
                            {
                                this.Invoke((EventHandler)(delegate
                                {
                                    WriteInfo("达到门限范围!");
                                }));
                                break;
                            }
                            k = 0;
                            Thread.Sleep(5);
                        }
                    }
                    i = 0;
                    Thread.Sleep(5);
                }
            }
            else//数据引导
            {
                for (int i = 0; i < 10; i++)
                {
                    if (zxBool == false) { break; }
                    if (DateTime.Now >= dqjs)
                    {
                        AxisControl.setMove(0, 0);
                        AxisControl.setMove(1, 0);
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("跟踪结束！");
                        }
         ));
                        Thread.Sleep(5000);
                        wZzxBool = true;
                        this.Invoke((EventHandler)(delegate
                        {
                            btnTxsc_Click(null, null);
                        }
                     ));
                        break;
                    }
                    if (csName.boolAGC == true)//LOCK
                    {
                        //X轴
                        if (csName.fwSk == false)//X轴记跟
                        {
                            double doJd = ydSjFw[1];
                            double doJd_1 = Math.Abs(doJd - (csName.fWySjD - 1800));
                            double doJd_2 = Math.Abs(doJd + 360 - (csName.fWySjD - 1800));
                            double doJd_3 = Math.Abs(doJd - 360 - (csName.fWySjD - 1800));
                            double[] array = new double[] { doJd_1, doJd_2, doJd_3 };
                            if (array.Min() == doJd_2) { doJd = doJd + 360; }
                            else if (array.Min() == doJd_3) { doJd = doJd - 360; }
                            double Gs = ydSjFw[0] - ydSjFw[1];
                            if (Math.Abs(Gs) >= 350) { if (Gs > 0) { Gs = Gs - 360; } else { Gs = 360 - Gs; } }
                            double G = Convert.ToDouble(csName.pl[31, 1].Split(',')[0]);
                            double P = Convert.ToDouble(csName.pl[31, 1].Split(',')[1]);
                            double I = Convert.ToDouble(csName.pl[31, 1].Split(',')[2]);
                            double D = Convert.ToDouble(csName.pl[31, 1].Split(',')[3]);
                            wcFwL[2] = wcFwL[1];
                            wcFwL[1] = wcFwL[0];
                            wcFwL[0] = doJd - (csName.fWySjD - 1800);
                            double Output = P * (wcFwL[0] - wcFwL[1]) + I * wcFwL[0] + D * (wcFwL[0] - wcFwL[1] * 2 + wcFwL[2]);
                            double outFW = (Gs * G + Output * 10) * csName.fwXs + csName.fwPy;
                            AxisControl.setSpeed(0, outFW);//X轴速度
                        }
                        //Y轴
                        if (csName.fySk == false)//Y轴记跟
                        {
                            double Gs = ydSjFy[0] - ydSjFy[1];
                            double G = Convert.ToDouble(csName.pl[32, 1].Split(',')[0]);
                            double P = Convert.ToDouble(csName.pl[32, 1].Split(',')[1]);
                            double I = Convert.ToDouble(csName.pl[32, 1].Split(',')[2]);
                            double D = Convert.ToDouble(csName.pl[32, 1].Split(',')[3]);
                            wcFyL[2] = wcFyL[1];
                            wcFyL[1] = wcFyL[0];
                            wcFyL[0] = ydSjFy[1] - csName.fYjD;
                            double Output = P * (wcFyL[0] - wcFyL[1]) + I * wcFyL[0] + D * (wcFyL[0] - wcFyL[1] * 2 + wcFyL[2]);
                            double outFY = (Gs * G + Output * 10) * csName.fyXs + csName.fyPy;
                            AxisControl.setSpeed(1, -outFY);//Y轴速度
                        }
                    }
                    else//unLOCK
                    {
                        if (fwSmColor == true || fySmColor == true)
                        {
                            for (int j = 0; j < 10; j++)
                            {
                                if (zxBool == false) { break; }
                                if (smGet == true)
                                {
                                    smGet = false;
                                    txsmGraphThread = new Thread(txsm){ IsBackground = true };
                                    txsmGraphThread.Start();
                                }
                                if (smtc == true)
                                {
                                    smtc = false;
                                    smGet = true;
                                    break;
                                }
                                j = 0;
                                Thread.Sleep(5);
                            }
                        }
                        if (smSb == true)
                        {
                            smSb = false;
                            this.Invoke((EventHandler)(delegate
                            {
                                WriteInfo("继续按原定轨道跟踪!");
                            }
                 ));
                        }
                        for (int k = 0; k < 10; k++)
                        {
                            if (zxBool == false) { break; }
                            if (DateTime.Now >= dqjs) { break; }
                            if (csName.boolAGC == false)//扫描失败执行
                            {
                                //X轴
                                if (csName.fwSk == false)//X轴记跟
                                {
                                    double doJd = ydSjFw[1];
                                    double doJd_1 = Math.Abs(doJd - (csName.fWySjD - 1800));
                                    double doJd_2 = Math.Abs(doJd + 360 - (csName.fWySjD - 1800));
                                    double doJd_3 = Math.Abs(doJd - 360 - (csName.fWySjD - 1800));
                                    double[] array = new double[] { doJd_1, doJd_2, doJd_3 };
                                    if (array.Min() == doJd_2) { doJd = doJd + 360; }
                                    else if (array.Min() == doJd_3) { doJd = doJd - 360; }
                                    double Gs = ydSjFw[0] - ydSjFw[1];
                                    if (Math.Abs(Gs) >= 350) { if (Gs > 0) { Gs = Gs - 360; } else { Gs = 360 - Gs; } }
                                    double G = Convert.ToDouble(csName.pl[31, 1].Split(',')[0]);
                                    double P = Convert.ToDouble(csName.pl[31, 1].Split(',')[1]);
                                    double I = Convert.ToDouble(csName.pl[31, 1].Split(',')[2]);
                                    double D = Convert.ToDouble(csName.pl[31, 1].Split(',')[3]);
                                    wcFwL[2] = wcFwL[1];
                                    wcFwL[1] = wcFwL[0];
                                    wcFwL[0] = doJd - (csName.fWySjD - 1800);
                                    double Output = P * (wcFwL[0] - wcFwL[1]) + I * wcFwL[0] + D * (wcFwL[0] - wcFwL[1] * 2 + wcFwL[2]);
                                    double outFW = (Gs * G + Output * 10) * csName.fwXs + csName.fwPy;
                                    AxisControl.setSpeed(0, outFW);//X轴速度
                                }
                                //Y轴
                                if (csName.fySk == false)//Y轴记跟
                                {
                                    double Gs = ydSjFy[0] - ydSjFy[1];
                                    double G = Convert.ToDouble(csName.pl[32, 1].Split(',')[0]);
                                    double P = Convert.ToDouble(csName.pl[32, 1].Split(',')[1]);
                                    double I = Convert.ToDouble(csName.pl[32, 1].Split(',')[2]);
                                    double D = Convert.ToDouble(csName.pl[32, 1].Split(',')[3]);
                                    wcFyL[2] = wcFyL[1];
                                    wcFyL[1] = wcFyL[0];
                                    wcFyL[0] = ydSjFy[1] - csName.fYjD;
                                    double Output = P * (wcFyL[0] - wcFyL[1]) + I * wcFyL[0] + D * (wcFyL[0] - wcFyL[1] * 2 + wcFyL[2]);
                                    double outFY = (Gs * G + Output * 10) * csName.fyXs + csName.fyPy;
                                    AxisControl.setSpeed(1, -outFY);//Y轴速度
                                }
                            }
                            else
                            {
                                this.Invoke((EventHandler)(delegate
                                {
                                    WriteInfo("达到门限范围!");
                                }
     ));
                                break;
                            }
                            k = 0;
                            Thread.Sleep(1);
                        }
                    }
                    i = 0;
                    Thread.Sleep(1);
                }
            }
        }

        //天线螺旋扫描
        double fw0 = 0;//扫描初始X轴角
        double fy0 = 0;//扫描初始Y轴角

        public void sxsm()
        {
            double fwman = csName.fWySjD + Convert.ToDouble(csName.pl[14, 1]);//X轴最大值
            if (fwman > Convert.ToDouble(csName.pl[19, 1]) + 1440) { fwman = Convert.ToDouble(csName.pl[19, 1]) + 1440; }
            double fwmin = csName.fWySjD - Convert.ToDouble(csName.pl[14, 1]);//X轴最小值
            if (fwmin < Convert.ToDouble(csName.pl[20, 1]) + 1440) { fwmin = Convert.ToDouble(csName.pl[20, 1]) + 1440; }
            double fyman = csName.fYjD + Convert.ToDouble(csName.pl[15, 1]);//Y轴最大值
            if (fyman > Convert.ToDouble(csName.pl[21, 1])) { fyman = Convert.ToDouble(csName.pl[21, 1]); }
            double fymin = csName.fYjD - Convert.ToDouble(csName.pl[15, 1]);//Y轴最小值
            if (fymin < Convert.ToDouble(csName.pl[22, 1])) { fymin = Convert.ToDouble(csName.pl[22, 1]); }
            double t = 0;//扫描初始时间
            double lJ = 0.5;//扫描螺距
            double fw = fw0;//X轴角给定
            double fy = fy0;//Y轴角给定
            double wFw = (Convert.ToDouble(csName.pl[12, 1]));//X轴角速度
            double wFy = (Convert.ToDouble(csName.pl[13, 1]));//Y轴角速度
            double pd = Convert.ToInt32(csName.pl[28, 1]) * 500;//扫描时长
            this.Invoke((EventHandler)(delegate
            {
                WriteInfo("超出门限范围，开始扫描！");
            }
     ));
            for (int i = 0; i < 100; i++)
            {
                if (zxBool == false) { break; }
                if (csName.boolAGC == true)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("扫描成功！");
                        double a = csName.fWYsjD - csName.fWjD;
                        fwXz.Text = a.ToString();
                        double b = csName.fYYsjD - csName.fYjD;
                        fyXz.Text = b.ToString();
                        角度修正toolStripMenuItem6_DropDownClosed(null, null);
                    }
                    ));
                    smtc = true;
                    break;
                }
                //if (fw >= fwmin && fw <= fwman && fy >= fymin && fy <= fyman) { t++; } else { t--; }
                t = 1; ;
                fw = fw0 + ((lJ * 10 * t) / Math.PI * 2) * Math.Cos(10 * t);//X轴角给定
                fy = fy0 + ((lJ * 10 * t) / Math.PI * 2) * Math.Sin(10 * t);//Y轴角给定
                //X轴扫描速度
                if (fwSmColor == true)
                {
                    double wzC = (fw - csName.fWySjD);//速度
                    //csAll.fwOut(wzC, chDevice, 0, 0, 1.15, 1.15, 5,5);
                }
                if (fwSmColor == true)
                {
                    double wzC = csName.fYjyjD - csName.fYjD;//速度
                    //csAll.fyOut(wzC, chDevice, 0.00, 0.00, 1.6, 1.6, 5,5);
                }
                Thread.Sleep(5);
            }
            smtc = true;
            this.Invoke((EventHandler)(delegate
            {
                WriteInfo("扫描失败,继续按原定轨道跟踪!");
            }
                  ));
        }
        #endregion

        #region 数据记录
        private void btnSjjl_Click(object sender, EventArgs e)
        {
            if (btnSjjl.BackColor == Color.Silver)
            {
                jlGraphThread = new Thread(sjJlxMl);
                jlGraphThread.Start();
                WriteInfo("数据记录功能开启!");
                btnSjjl.BackColor = Color.Lime;
            }
            else
            {
                jlGraphThread.Abort();
                btnSjjl.BackColor = Color.Silver;
                WriteInfo("数据记录功能关闭!");
            }
        }
        //数据记录事件
        Thread jlGraphThread;
        public void sjJlxMl()
        {
            for (int i = 0; i < 10; i++)
            {
                if (csName.pl[23, 1] == "程序跟踪" && csName.fYYsjD >= 0.001)
                {
                    FileStream fs1;
                    StreamWriter sw;
                    string lj = csName.pl[26, 1] + "\\" + csName.mWxName + DateTime.Now.ToString("yyyy年MM月dd日HH.mm.ss.fff") + ".txt";
                    if (!Directory.Exists(csName.pl[26, 1]))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("未找到存储路径,请修改后再记录!");
                            btnSjjl.BackColor = Color.Silver;
                        }
                         ));
                        break;
                    }
                    else
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("数据记录中...");
                        }
                         ));
                    }
                    fs1 = new FileStream(lj, FileMode.Create, FileAccess.Write);
                    File.SetAttributes(@lj, FileAttributes.Compressed);
                    sw = new StreamWriter(fs1);
                    sw.WriteLine("" + "\r\n");
                    sw.WriteLine(@"时间                       实际X轴   实际Y轴    预设X轴    预设Y轴   X轴误差  Y轴误差" + "\r\n");
                    //sw.WriteLine(@"时间                       实际X轴   实际Y轴    预设X轴    预设Y轴   X轴误差  Y轴误差  X轴速度  Y轴速度" + "\r\n");
                    for (int j = 0; j < 1000000; j++)
                    {
                        if (csName.fYYsjD == 0)
                        {
                            this.Invoke((EventHandler)(delegate
                            {
                                WriteInfo("数据记录结束!");

                            }
                                ));
                            break;
                        }
                        string sj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
                        double sjfw = csName.fWjD;
                        double sjfy = csName.fYjD;
                        double ysfw = csName.fWYsjD;
                        double ysfy = csName.fYYsjD;
                        string fwwc = Math.Abs((ysfw - sjfw)).ToString("0.0000");
                        string fywc = Math.Abs((ysfy - sjfy)).ToString("0.0000");
                        //string fwwc = (ysfw - sjfw).ToString("0.0000");
                        //string fywc = (ysfy - sjfy).ToString("0.0000");
                        //sw.WriteLine(sj + ",   " + sjfw.ToString("000.0000") + ",   " + sjfy.ToString("00.0000") + ",   " + ysfw.ToString("000.0000") + ",   " + ysfy.ToString("00.0000") + ",   " + fwwc + ",   " + fywc + ",   " + csName.sdssFw + ",   " + csName.sdssFy);//开始写入值
                        sw.WriteLine(sj + ",   " + sjfw.ToString("000.0000") + ",   " + sjfy.ToString("00.0000") + ",   " + ysfw.ToString("000.0000") + ",   " + ysfy.ToString("00.0000") + ",   " + fwwc + ",   " + fywc);//开始写入值
                        j = 0;
                        if (btnSjjl.BackColor == Color.Silver)
                        {
                            sw.Close();
                            fs1.Close();
                            break;
                        }
                        Thread.Sleep(Convert.ToInt32(csName.pl[27, 1]) - 10);// 时间间隔
                    }
                    sw.Close();
                    fs1.Close();
                }
                else if (csName.pl[23, 1] == "数据引导" && DateTime.Now <= Convert.ToDateTime(csName.jgNew[2].Substring(5)) && DateTime.Now >= Convert.ToDateTime(csName.jgNew[1].Substring(5)))
                {
                    FileStream fs1;
                    StreamWriter sw;
                    string lj = csName.pl[26, 1] + "\\" + DateTime.Now.ToString("yyyy年MM月dd日HH.mm.ss.fff") + ".txt";
                    if (!Directory.Exists(csName.pl[26, 1]))
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("未找到存储路径,请修改后再记录!");
                            btnSjjl.BackColor = Color.Silver;
                        }

             ));
                        break;
                    }
                    else
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("数据记录中...");
                        }
             ));
                    }
                    fs1 = new FileStream(lj, FileMode.Create, FileAccess.Write);
                    File.SetAttributes(@lj, FileAttributes.Compressed);
                    sw = new StreamWriter(fs1);
                    sw.WriteLine("" + "\r\n");
                    //sw.WriteLine(@"时间                       实际X轴   实际Y轴    引导X轴    引导Y轴   X轴误差  Y轴误差  X轴速度  Y轴速度" + "\r\n");
                    sw.WriteLine(@"时间                       实际X轴   实际Y轴    引导X轴    引导Y轴   X轴误差  Y轴误差" + "\r\n");
                    for (int j = 0; j < 1000000; j++)
                    {
                        if (DateTime.Now >= Convert.ToDateTime(csName.jgNew[2].Substring(5)))
                        {
                            this.Invoke((EventHandler)(delegate
                            {
                                WriteInfo("数据记录结束!");

                            }
              ));
                            break;
                        }
                        string sj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
                        double sjfw = csName.fWjD;
                        double sjfy = csName.fYjD;
                        double ysfw = csName.fWjyjD;
                        double ysfy = csName.fYjyjD;
                        string fwwc = Math.Abs(ysfw - sjfw).ToString("0.0000");
                        string fywc = Math.Abs(ysfy - sjfy).ToString("0.0000");
                        //string fwwc = (ysfw - sjfw).ToString("0.0000");
                        //string fywc = (ysfy - sjfy).ToString("0.0000");
                        //sw.WriteLine(sj + ",   " + sjfw.ToString("000.0000") + ",   " + sjfy.ToString("00.0000") + ",   " + ysfw.ToString("000.0000") + ",   " + ysfy.ToString("00.0000") + ",   " + fwwc + ",   " + fywc + ",   " + csName.sdssFw + ",   " + csName.sdssFy);//开始写入值
                        sw.WriteLine(sj + ",   " + sjfw.ToString("000.0000") + ",   " + sjfy.ToString("00.0000") + ",   " + ysfw.ToString("000.0000") + ",   " + ysfy.ToString("00.0000") + ",   " + fwwc + ",   " + fywc);//开始写入值

                        j = 0;
                        if (btnSjjl.BackColor == Color.Silver)
                        {
                            sw.Close();
                            fs1.Close();
                            break;
                        }
                        Thread.Sleep(Convert.ToInt32(csName.pl[27, 1]) - 10);// 时间间隔
                    }
                    sw.Close();
                    fs1.Close();
                }
                Thread.Sleep(10);
                i = 0;
            }
        }
        #endregion

        #region 综合信息显示
        //远程控制


        //通道门限
        private void btnTdmx_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
            WriteInfo("当前通道门限,上限：" + csName.pl[6, 1] + "V;下限：" + csName.pl[7, 1] + "V.");
        }

        #endregion

        #region 天线开关机(PCI1730-IO输出)
        //X轴开机
        private void btnFwkj_Click(object sender, EventArgs e)
        {
            AxisControl.setEnable(0, true);
            Thread.Sleep(200);
            WriteInfo("方位轴已开机！");
            btnFwkj.BackColor = Color.Lime;
            btnFwdj.BackColor = Color.Silver;
            btnFwkj.Enabled = false;
            btnFwdj.Enabled = true;
        }
        //X轴待机
        private void btnFwdj_Click(object sender, EventArgs e)
        {
            AxisControl.setEnable(0, false);
            btnFwdj.BackColor = Color.Lime;
            btnFwkj.BackColor = Color.Silver;
            btnFwdj.Enabled = false;
            btnFwkj.Enabled = true;
            WriteInfo("方位轴待机中！");
        }
        //Y轴开机
        private void btnFykj_Click(object sender, EventArgs e)
        {
            AxisControl.setEnable(1, true);
            Thread.Sleep(200); WriteInfo("俯仰轴已开机！");
            btnFykj.BackColor = Color.Lime;
            btnFydj.BackColor = Color.Silver;
            btnFykj.Enabled = false;
            btnFydj.Enabled = true;
        }
        //Y轴待机
        private void btnFydj_Click(object sender, EventArgs e)
        {
            AxisControl.setEnable(1, false);
            btnFydj.BackColor = Color.Lime;
            btnFykj.BackColor = Color.Silver;
            btnFydj.Enabled = false;
            btnFykj.Enabled = true;
            WriteInfo("俯仰轴待机中...");
        }
        #endregion

        #region 天线手动控制模式与天线自动扫描模式
        //X轴手速
        private void btnFwss_Click(object sender, EventArgs e)
        {
            if (btnFwss.BackColor == Color.Silver)
            {
                btnFwss.BackColor = Color.Lime;
                fwAdd.Enabled = true;
                fwSubtract.Enabled = true;
                WriteInfo("方位轴手速功能开启！");
            }
            else
            {
                btnFwss.BackColor = Color.Silver;
                fwAdd.Enabled = false;
                fwSubtract.Enabled = false;
                WriteInfo("方位轴手速功能禁用！");
            }
        }
        //Y轴手速
        private void btnFyss_Click(object sender, EventArgs e)
        {
            if (btnFyss.BackColor == Color.Silver)
            {
                btnFyss.BackColor = Color.Lime;
                fyAdd.Enabled = true;
                fySubtract.Enabled = true;
                WriteInfo("俯仰轴手速功能开启！");
            }
            else
            {
                btnFyss.BackColor = Color.Silver;
                fyAdd.Enabled = false;
                fySubtract.Enabled = false;
                WriteInfo("俯仰轴手速功能禁用！");
            }
        }
        //X轴扫描
        bool fwSmColor = false;
        private void btnFwsm_Click(object sender, EventArgs e)
        {
            if (btnFwsm.BackColor == Color.Silver)
            {
                btnFwsm.BackColor = Color.Lime;
                btnFwsk.BackColor = Color.Silver;
                btnLeft.Enabled = false;
                btnRight.Enabled = false;
                WriteInfo("方位轴扫描功能开启！");
                fwSmColor = true;
            }
        }
        //Y轴扫描
        bool fySmColor = false;
        private void btnFysm_Click(object sender, EventArgs e)
        {
            if (btnFysm.BackColor == Color.Silver)
            {
                btnFysm.BackColor = Color.Lime;
                btnFysk.BackColor = Color.Silver;
                btnTop.Enabled = false;
                btnBottom.Enabled = false;
                WriteInfo("俯仰轴扫描功能开启！");
                fySmColor = true;
            }
        }
        //X轴手控
        private void btnFwsk_Click(object sender, EventArgs e)
        {
            if (btnFwsk.BackColor == Color.Silver)
            {
                btnFwsk.BackColor = Color.Lime;
                btnFwsm.BackColor = Color.Silver;
                btnLeft.Enabled = true;
                btnRight.Enabled = true;
                WriteInfo("方位轴手控功能开启！");
                fwSmColor = false;
            }
        }
        //Y轴手控
        private void btnFysk_Click(object sender, EventArgs e)
        {
            if (btnFysk.BackColor == Color.Silver)
            {
                btnFysk.BackColor = Color.Lime;
                btnFysm.BackColor = Color.Silver;
                btnTop.Enabled = true;
                btnBottom.Enabled = true;
                WriteInfo("俯仰轴手控功能开启！");
                fySmColor = false;
            }
        }
        #endregion

        #region 命令执行取消,天线收藏
        //X轴命令
        private void btnFwml_Click(object sender, EventArgs e)
        {
            if (btnFwml.BackColor == Color.Lime)
            {
                txtFwml.Enabled = false;
                btnFwml.BackColor = Color.Silver;
                WriteInfo("方位轴命令功能关闭！");
            }
            else
            {
                txtFwml.Enabled = true;
                btnFwml.BackColor = Color.Lime;
                WriteInfo("方位轴命令功能开启！");
            }
        }
        //Y轴命令
        private void btnFyml_Click(object sender, EventArgs e)
        {
            if (btnFyml.BackColor == Color.Lime)
            {
                txtFyml.Enabled = false;
                btnFyml.BackColor = Color.Silver;
                WriteInfo("俯仰轴命令功能关闭！");
            }
            else
            {
                txtFyml.Enabled = true;
                btnFyml.BackColor = Color.Lime;
                WriteInfo("俯仰轴命令功能开启！");
            }
        }
        //天线收藏
        private void btnTxsc_Click(object sender, EventArgs e)
        {
            if (btnTxsc.BackColor == Color.Silver)
            {
                if (btnFydj.Enabled == true || btnFwdj.Enabled == true)
                {
                    txtFwml.Text = "0";
                    txtFyml.Text = "0";
                    boolFw = boolFy = 1;
                    zxFwGraphThread = new Thread(ZxFwMl);
                    zxFwGraphThread.Start();
                    zxFyGraphThread = new Thread(ZxFyMl);
                    zxFyGraphThread.Start();
                    WriteInfo("天线收藏命令执行中...");
                    btnTxsc.BackColor = Color.Lime;
                }
                else
                {
                    WriteInfo("天线未开机！");
                }
            }
            else
            {
                boolFw = boolFy = 0;
                WriteInfo("天线收藏命令取消！");
                btnTxsc.BackColor = Color.Silver;
                AxisControl.setMove(0, 0);
                AxisControl.setMove(1, 0);

            }
        }

        #endregion

        #region 跟踪模式按钮
        private void btnFwcg_Click(object sender, EventArgs e)//X轴程跟
        {

        }
        private void btnFwzg_Click(object sender, EventArgs e)//X轴数引
        {

        }
        private void btnFycg_Click(object sender, EventArgs e) //Y轴程跟
        {

        }
        private void btnFyzg_Click(object sender, EventArgs e)//Y轴记跟
        {

        }
        #endregion

        #region 子窗口
        private void 伺服测试ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
        private void 卫星接收轨道时间段ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (gzMs.Text == "程序跟踪")
            {
                string str;
                string[] ar = new string[1000];
                StreamReader dcrd;
                if (csName.pl[1, 1] == "多星任务")
                {
                    str = "\\text\\most2.txt";
                }
                else
                {
                    str = "\\text\\OneCl2.txt";
                }
                dcrd = File.OpenText(Environment.CurrentDirectory + str);
                string lin = dcrd.ReadLine();
                int ii = 0;
                if (lin.Substring(0, 4) == "开始时间")
                {
                    string lines = dcrd.ReadLine();
                    ar[0] = 1 + "_" + lin.Substring(0, 28) + "--" + lines.Substring(0, 28).ToString();
                    ii = 1;
                }
                for (int i = ii; i < 1000; i++)  //读入数据并赋予数组
                {
                    string line = dcrd.ReadLine();
                    if (line == null) { break; }
                    string lines = dcrd.ReadLine();
                    if (lines == null) { break; }
                    ar[i] = ((i + 1) + "_" + line.Substring(0, 28) + "--" + lines.Substring(0, 28)).ToString();
                }
                dcrd.Close();
                MessageBox.Show(string.Join("\r\n", ar).Trim(), lblWx.Text.ToString());
            }
            else
            {
                MessageBox.Show(csName.jgNew[1] + "--" + csName.jgNew[2]);
            }
        }

        private void 预置位置修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            add f2 = new add();
            f2.ShowDialog();
            if (f2.DialogResult == DialogResult.OK)
            {
                cbYzwz.Items.Clear();
                StreamReader dry = File.OpenText(Environment.CurrentDirectory + "\\text\\txYzwz.txt");
                for (int i = 0; i < 1000; i++)
                {
                    if (dry.EndOfStream) { break; }
                    csName.yzWz[i] = dry.ReadLine();
                    cbYzwz.Items.Add(csName.yzWz[i].Substring(0, csName.yzWz[i].IndexOf(',', 0)));
                }
                cbYzwz.SelectedIndex = 0;
                dry.Close();
                dry.Dispose();
            }
        }
        xlJs f2 = new xlJs();
        jkx.gsjs f3 = new jkx.gsjs();
        dGWX f4 = new dGWX();
        private void 天线姿态解算ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (zxBool == false)
            {
                f2.ShowDialog();
                if (f2.DialogResult == DialogResult.OK)
                {
                    jsGraphThread = new Thread(jsGraph) { IsBackground = true };
                    jsGraphThread.Start();
                    WriteInfo("天线姿态解算中...");
                    天线姿态解算ToolStripMenuItem.Enabled = false;
                }
            }
            else
            {
                MessageBox.Show("请先取消\"自动执行\"!", "消息框");
            }
        }
        Thread jsGraphThread;
        public void jsGraph()
        {
            //经纬度
            string strJWH = csName.pl[0, 1];
            strArray = strJWH.Split(',');
            double dbWD = Convert.ToDouble(strArray[0]);
            double dbJD = Convert.ToDouble(strArray[2]);
            double dbGD = Convert.ToDouble(strArray[4]);
            try
            {
                //需记录数据
                string[] all = new string[] { "0", "0", "0", "0", "0" };
                DateTime daq1 = Convert.ToDateTime(f2.Str[3]);
                DateTime daq2 = Convert.ToDateTime(f2.Str[4]);
                ArrayList dataList = ayxjClass.timeAngle(f2.Str[0], f2.Str[1], f2.Str[2], dbWD, dbJD, dbGD, daq1, daq2, Convert.ToInt32(f2.Str[7]), 8);
                FileStream fs = new FileStream(f2.Str[6] + "\\" + f2.Str[0] + "_" + Convert.ToDateTime(f2.Str[3]).ToString("yyyy年MM月dd日HH时mm分ss.fff秒") + ".txt", FileMode.Create);
                File.SetAttributes(f2.Str[6] + "\\" + f2.Str[0] + "_" + Convert.ToDateTime(f2.Str[3]).ToString("yyyy年MM月dd日HH时mm分ss.fff秒") + ".txt", FileAttributes.Compressed);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write("卫星名:" + f2.Str[0] + "\r\n开始时间:" + f2.Str[3] + "\r\n结束时间:" + f2.Str[4] + "\r\n解算间隔(ms):" + f2.Str[7] + "\r\n\r\n");
                sw.Write(@"时间                                          X轴角             Y轴角" + "\r\n");
                for (int i = 0; i < 999999999; i++)
                {
                    try
                    { sw.Write(dataList[i] + "\r\n"); }
                    catch {; break; }
                }
                this.Invoke((EventHandler)(delegate
                {
                    WriteInfo("天线姿态解算完成！");
                    天线姿态解算ToolStripMenuItem.Enabled = true;

                }));
                //清空缓冲区
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
            catch
            {
                this.Invoke((EventHandler)(delegate
                {
                    天线姿态解算ToolStripMenuItem.Enabled = true;
                }));
                MessageBox.Show("解算出错,请检查数据是否有误!", "消息框");
            }
        }
        #endregion

        #region 指示灯
        private void pbfwyk_Paint(object sender, PaintEventArgs e)
        {
            controlPaint.paintPictureBox((Control)sender);
        }

        #endregion



        #region 星历处理菜单
        string filePath = Environment.CurrentDirectory + @"\text\\SatelliteData";
        //单星星历更新
        private void 更改信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (zxBool != true)
                {
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        WriteInfo("单星星历更新中...");
                        //读文挡
                        csName.dxlAdress = Path.GetFullPath(openFileDialog1.FileName);
                        using (StreamReader rd = File.OpenText(csName.dxlAdress))
                        {
                            for (int i = 0; i < 3; i++)  //读入数据并赋予数组
                            {
                                string line = rd.ReadLine();
                                csName.dXl[i] = line;
                            }
                        }
                        //更新星历文档
                        string fileFullPath = filePath + @"\" + csName.dXl[0] + ".txt";
                        using (FileStream file = File.Open(fileFullPath, FileMode.OpenOrCreate))
                        {
                            using (StreamWriter SW = new StreamWriter(file))
                            {
                                SW.WriteLine(csName.dXl[0]);
                                SW.WriteLine(csName.dXl[1]);
                                SW.WriteLine(csName.dXl[2]);
                                SW.Flush();
                            }
                        }
                        //写星历名
                        this.tcwxNames.Items.Clear();
                        this.tcwxNames.Items.Add(csName.dXl[0]);
                        this.tcwxNames.Text = this.tcwxNames.Items[0].ToString();
                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "卫星名称:" + csName.pl[2, 1], "卫星名称:" + tcwxNames.Text);
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[2, 1] = lblWx.Text = label54.Text = tcwxNames.Text;
                        }
                        WriteInfo("单星星历更新完成！");
                        数据更新ToolStripMenuItem_Click(null, null);
                    }
                }
                else
                {
                    WriteInfo("请先关闭'自动执行'！");
                }
            }
            catch (Exception err)
            { WriteInfo(err.ToString()); }
        }
        //声明单星处理工作线程
        Thread oneGraphThread;
        //单星任务数据更新
        private void 数据更新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (zxBool != true)
            {
                oneGraphThread = new Thread(oneGraph) { IsBackground = true }; oneGraphThread.Start();
            }
            else { WriteInfo("请先关闭'自动执行'！"); }
        }
        //单星数据处理线程
        private void oneGraph()
        {
            kg = 0;
            this.Invoke((EventHandler)(delegate
            {
                WriteInfo("单星数据处理中...");
            }
               ));
            string[] strDx = csName.dXl;
            string strJWH = csName.pl[0, 1];
            strArray = strJWH.Split(',');
            double dbWD = Convert.ToDouble(strArray[0]);
            double dbJD = Convert.ToDouble(strArray[2]);
            double dbGD = Convert.ToDouble(strArray[4]);
            DateTime daq = DateTime.Now;

            //星历计算

            ArrayList strs = ayxjClass.timeInterval(strDx[0], strDx[1], strDx[2], dbWD, dbJD, dbGD, daq, 8, 1);

            FileStream fss = new FileStream(Environment.CurrentDirectory + "\\text\\OneCl2.txt", FileMode.Create);
            StreamWriter sww = new StreamWriter(fss);
            for (int i = 0; i < strs.Count; i++)
            {
                sww.Write(strs[i]);
                csName.dtimfNew[i] = strs[i].ToString();
            }
            sww.Flush();
            sww.Close();
            fss.Close();
            csName.dfk = 0;
            this.Invoke((EventHandler)(delegate { WriteInfo("单星数据更新完成!"); }));
            kg = 1;
        }
        ///
        ///
        //多星星历更新
        private void 星历更新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (zxBool != true)
                {
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        WriteInfo("多星星历更新中...");
                        string satelliteName = "";
                        string firstLine = "";
                        string secondLine = "";
                        using (FileStream FS = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            using (StreamReader SR = new StreamReader(FS))
                            {
                                while (!SR.EndOfStream)
                                {
                                    string str = SR.ReadLine();
                                    if (str.Length > 1 && str[0] != '1' && str[1] != '2')
                                    {
                                        satelliteName = str;
                                        if (!SR.EndOfStream)
                                        {
                                            str = SR.ReadLine();
                                            if (str.Length > 0 && str[0] == '1')
                                            {
                                                firstLine = str;
                                                if (!SR.EndOfStream)
                                                {
                                                    str = SR.ReadLine();
                                                    if (str.Length > 0 && str[0] == '2')
                                                    {
                                                        secondLine = str;
                                                        ///写一个卫星数据
                                                        string fileFullPath = filePath + @"\" + satelliteName + ".txt";
                                                        using (FileStream file = File.Open(fileFullPath, FileMode.OpenOrCreate))
                                                        {
                                                            using (StreamWriter SW = new StreamWriter(file))
                                                            {
                                                                SW.WriteLine(satelliteName); SW.WriteLine(firstLine); SW.WriteLine(secondLine); SW.Flush();
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "卫星名称:" + csName.pl[2, 1], "卫星名称:" + tcwxNames.Text);
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[2, 1] = lblWx.Text = label54.Text = tcwxNames.Text;
                        }
                        WriteInfo("多星星历更新完成！");
                        数据ToolStripMenuItem_Click(sender, e);
                    }
                }
                else { WriteInfo("请先关闭'自动执行'！"); }
            }
            catch (Exception err)
            { WriteInfo(err.ToString()); }
        }

        //多星任务数据更新
        private void 数据ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (zxBool != true)
            {
                mostGraphThread = new Thread(mostGraph) { IsBackground = true }; mostGraphThread.Start();
            }
            else { WriteInfo("请先关闭'自动执行'！"); }
        }
        //声明多星工作线程
        Thread mostGraphThread;
        //多星数据处理线程
        private void mostGraph()
        {
            kg = 0;
            this.Invoke((EventHandler)(delegate
            {
                WriteInfo("多星数据处理中...");
            }
               ));
            string[] strDx = new string[9999];
            StreamReader rd = File.OpenText(Environment.CurrentDirectory + "\\text\\most.txt");
            for (int i = 0; i < 9999; i++)  //读入数据并赋予数组
            {
                strDx[i] = rd.ReadLine();
                if (strDx[i] == null) { break; }
            }
            rd.Close();
            int z = 0;
            this.Invoke((EventHandler)(delegate
            {
                z = tcwxNames.Items.IndexOf(tcwxNames.Text);
            }
            ));
            string strJWH = csName.pl[0, 1];
            strArray = strJWH.Split(',');
            double dbWD = Convert.ToDouble(strArray[0]);
            double dbJD = Convert.ToDouble(strArray[2]);
            double dbGD = Convert.ToDouble(strArray[4]);
            DateTime daq = DateTime.Now;
            ArrayList strs = ayxjClass.timeInterval(strDx[3 * z], strDx[3 * z + 1], strDx[3 * z + 2], dbWD, dbJD, dbGD, daq, 8, 1);
            FileStream fss = new FileStream(Environment.CurrentDirectory + "\\text\\most2.txt", FileMode.Create);
            StreamWriter sww = new StreamWriter(fss);
            for (int i = 0; i < strs.Count; i++)
            {
                sww.Write(strs[i]);
                csName.stimfNew[i] = strs[i].ToString();
            }
            sww.Flush();
            sww.Close();
            fss.Close();
            csName.sfk = 0;
            this.Invoke((EventHandler)(delegate
            {
                WriteInfo("多星数据更新完成!");
            }
               ));
            kg = 1;
        }
        #endregion

        #region 参数变更菜单
        //卫星选择
        private void tcwxNames_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (tbclfs.Text == "多星任务" && csName.pl[2, 1] != tcwxNames.Text)
                {
                    string strwx = csName.pl[2, 1];
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "卫星名称:" + strwx, "卫星名称:" + tcwxNames.Text);
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[2, 1] = lblWx.Text = label54.Text = tcwxNames.Text;
                    }
                    csName.wxSy = tcwxNames.Items.IndexOf(tcwxNames.Text);
                    WriteInfo("卫星切换为:" + csName.pl[2, 1] + ".");
                    //创建线程
                    mostGraphThread = new Thread(mostGraph);
                    //启动线程
                    mostGraphThread.Start();
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                WriteInfo("切换卫星失败！");
            }
        }
        bool tongbu = false;
        //同步卫星选择
        private void toolStripComboBox2_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (csName.pl[46, 1] != toolStripComboBox2.Text)
                {
                    string strwx = csName.pl[46, 1];
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "同步卫星名称:" + strwx, "同步卫星名称:" + toolStripComboBox2.Text);
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[46, 1] = lblWx.Text = label54.Text = toolStripComboBox2.Text; xingID = (UInt16)toolStripComboBox2.SelectedIndex;
                    }
                    tongbu = true;
                    WriteInfo("同步卫星切换为:" + csName.pl[46, 1] + ".");
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                WriteInfo("切换同步卫星失败！");
            }
        }
        UInt16 xingID = 0;
        //处理方式变更
        private void toolStripComboBox4_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                if (btnZdzx.BackColor == Color.Silver)
                {
                    if (csName.pl[1, 1] != tbclfs.Text)
                    {
                        if (tbclfs.Text == "单星任务")
                        {
                            tcwxNames.Items.Clear();
                            tcwxNames.Items.Add(csName.sxlm[0]);
                            tcwxNames.Text = lblWx.Text = label54.Text = csName.sxlm[0];//卫星名
                            单星任务ToolStripMenuItem.Enabled = true;
                            更改信息ToolStripMenuItem.Enabled = true;
                            数据更新ToolStripMenuItem.Enabled = true;
                            多星任务ToolStripMenuItem.Enabled = false;
                            星历更新ToolStripMenuItem.Enabled = false;
                            数据ToolStripMenuItem.Enabled = false;
                            WriteInfo("已切单星任务!");
                        }
                        else
                        {
                            tcwxNames.Items.Clear();
                            for (int i = 0; i < 9999; i++)
                            {
                                if (csName.sxlm[i] == null) { break; }
                                tcwxNames.Items.Add(csName.sxlm[i]);
                            }
                            this.tcwxNames.Text = lblWx.Text = label54.Text = csName.pl[2, 1];
                            单星任务ToolStripMenuItem.Enabled = false;
                            更改信息ToolStripMenuItem.Enabled = false;
                            数据更新ToolStripMenuItem.Enabled = false;
                            多星任务ToolStripMenuItem.Enabled = true;
                            星历更新ToolStripMenuItem.Enabled = true;
                            数据ToolStripMenuItem.Enabled = true;
                            WriteInfo("已切多星任务!");
                        }
                        string strwx = csName.pl[1, 1];
                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "处理方式:" + strwx, "处理方式:" + tbclfs.Text);
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[1, 1] = tbclfs.Text;
                        }
                        csName.dfk = 0;
                        csName.sfk = 0;
                    }
                }
                else
                {
                    MessageBox.Show("请取消\"自动执行\"后再进行切换!", "消息框");
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                WriteInfo("处理方式切换失败！");
            }
        }
        ////星历获取方式
        //private void 星历获取toolStripMenuItem5_DropDownClosed(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        string stsx = csName.pl[24, 1];
        //        string txtXx = "";
        //        if (csName.pl[24, 1] != xlHq.Text)
        //        {
        //            txtXx = xlHq.Text;
        //            string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
        //            if (File.Exists(strFilePath))
        //            {
        //                string strContent = File.ReadAllText(strFilePath);
        //                strContent = Regex.Replace(strContent, "星历获取:" + stsx, "星历获取:" + txtXx.ToString());
        //                File.WriteAllText(strFilePath, strContent);
        //                csName.pl[24, 1] = txtXx.ToString();
        //            }
        //            richTextBox1.Text = richTextBox1.Text + DateTime.Now.ToString("HH:mm:ss") + ">星历获取已修改为:" + csName.pl[24, 1] + "!\r\n";
        //        }
        //    }
        //    catch
        //    {
        //        tabControl1.SelectedIndex = 0;
        //        xlHq.Text = csName.pl[24, 1];
        //        richTextBox1.Text = richTextBox1.Text + DateTime.Now.ToString("HH:mm:ss") + ">星历获取方式切换失败！\r\n";
        //    }
        //}
        #region 监测监控
        private void 记跟数据更新toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (zxBool != true)
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    WriteInfo("引导数据更新中...");
                    //读文挡
                    csName.sjAdress = Path.GetFullPath(openFileDialog1.FileName);
                    StreamReader rd = File.OpenText(csName.sjAdress);

                    //更新星历文档
                    FileStream fs = new FileStream(Environment.CurrentDirectory + "\\text\\jyNew.txt", FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs);

                    for (int i = 0; i < 999999999; i++)  //读入数据并赋予数组
                    {
                        string line = rd.ReadLine();
                        if (line == null) { break; }
                        csName.jgNew[i] = line;
                        sw.Write(csName.jgNew[i] + "\r\n");
                    }
                    rd.Close();
                    //清空缓冲区
                    sw.Flush();
                    //关闭流
                    sw.Close();
                    fs.Close();
                    WriteInfo("引导数据更新中完成!");
                }
            }
            else
            {
                WriteInfo("请先关闭'自动执行'!");
            }
        }

        private void toolStripComboBox1_DropDownClosed_1(object sender, EventArgs e)
        {
            string stsx = csName.pl[38, 1];
            string txtXx = toolStripComboBox1.Text.Trim();
            if (csName.pl[38, 1] != toolStripComboBox1.Text)
            {
                string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                csName.pl[38, 1] = toolStripComboBox1.Text;
                if (toolStripComboBox1.Text == "手动更新")
                {
                    string strContent = File.ReadAllText(strFilePath);
                    strContent = Regex.Replace(strContent, "远端星历:" + stsx, "远端星历:" + txtXx.ToString());
                    File.WriteAllText(strFilePath, strContent);
                    csName.webSatelliteData = false; WriteInfo("星历更新切换为：手动更新!");
                }
                else
                {
                    string strContent = File.ReadAllText(strFilePath);
                    strContent = Regex.Replace(strContent, "远端星历:" + stsx, "远端星历:" + txtXx.ToString());
                    File.WriteAllText(strFilePath, strContent);
                    csName.webSatelliteData = true; WriteInfo("星历更新切换为：网络获取!");
                }
            }
        }

        private void boolYc_DropDownClosed(object sender, EventArgs e)
        {
            string stsx = csName.pl[24, 1];
            string txtXx = boolYc.Text.Trim();
            if (csName.pl[24, 1] != boolYc.Text)
            {
                string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                if (File.Exists(strFilePath))
                {
                    string strContent = File.ReadAllText(strFilePath);
                    strContent = Regex.Replace(strContent, "是否远程:" + stsx, "是否远程:" + txtXx.ToString());
                    File.WriteAllText(strFilePath, strContent);
                    csName.pl[24, 1] = txtXx.ToString();
                }
                if (txtXx == "是")
                {
                    ipAddress.Enabled = false; port.Enabled = false; inforVer.Enabled = false;
                    WriteInfo("已允许远程监控!");
                    ServerCon();
                }
                else
                {
                    ipAddress.Enabled = true; port.Enabled = true; inforVer.Enabled = true;
                    WriteInfo("已拒绝远程监控!");
                    boolServerSendMsg = false;
                    try
                    {
                        socketWatch.Close();
                        socConnection.Close();
                    }
                    catch { }
                    socketWatch = null;
                    socConnection = null;
                }
            }
        }
        private void iP地址ToolStripMenuItem_DropDownClosed(object sender, EventArgs e)//ip地址修改
        {
            string stsx = csName.pl[4, 1];
            string txtXx = ipAddress.Text;
            if (csName.pl[4, 1] != ipAddress.Text)
            {
                if (Regex.IsMatch(ipAddress.Text, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"))
                {
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "IP地址:" + stsx, "IP地址:" + txtXx.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[4, 1] = txtXx.ToString();
                    }
                    WriteInfo("IP地址已修改为:" + csName.pl[4, 1]);
                }
                else
                {
                    tabControl1.SelectedIndex = 0;
                    ipAddress.Text = csName.pl[4, 1];
                    WriteInfo("IP地址修改失败！");
                }
            }
        }
        private void 端口号ToolStripMenuItem_DropDownClosed(object sender, EventArgs e)//端口号变更
        {
            try
            {
                string stsx = csName.pl[5, 1];
                UInt16 txtXx = Convert.ToUInt16(port.Text);
                if (csName.pl[5, 1] != port.Text)
                {
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "端口号:" + stsx, "端口号:" + txtXx.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[5, 1] = txtXx.ToString();
                    }
                    WriteInfo("端口号已修改为:" + csName.pl[5, 1]);
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                port.Text = csName.pl[5, 1];
                WriteInfo("端口号修改失败！");
            }
        }

        private void toolStripMenuItem2_DropDownClosed(object sender, EventArgs e)//连接校验
        {
            try
            {
                string stsx = csName.pl[16, 1];
                string txtXx = inforVer.Text;
                if (csName.pl[16, 1] != inforVer.Text)
                {
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "连接校验:" + stsx, "连接校验:" + txtXx.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[16, 1] = txtXx.ToString();
                    }
                    WriteInfo("连接校验已修改为:" + csName.pl[16, 1]);

                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                inforVer.Text = csName.pl[16, 1];
                WriteInfo("连接校验修改失败！");
            }
        }
        #endregion

        //跟踪模式变更
        private void toolStripComboBox1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string stsx = csName.pl[23, 1];
                string txtXx = "";
                if (csName.pl[23, 1] != gzMs.Text)
                {
                    txtXx = gzMs.Text;
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "跟踪模式:" + stsx, "跟踪模式:" + txtXx.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[23, 1] = txtXx.ToString();
                    }
                    csName.mWxName = "";
                    csName.mQc = "";
                    csName.mksTime = "";
                    csName.mjsTime = "";
                    csName.fWYsjD = 0;
                    csName.fYYsjD = 0;
                    csName.fWjyjD = 0;
                    csName.fYjyjD = 0;
                    WriteInfo("跟踪模式已修改为:" + csName.pl[23, 1] + "!");
                }

            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                gzMs.Text = csName.pl[23, 1];
                WriteInfo("跟踪模式修改失败！");
            }
        }
        ////跟踪精度变更
        //private void 跟踪精度toolStripMenuItem1_DropDownClosed(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        string stsx = csName.pl[16, 1];
        //        string txtXx = "";
        //        if (csName.pl[16, 1] != gzJd.Text)
        //        {
        //            txtXx = gzJd.Text;
        //            string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
        //            if (File.Exists(strFilePath))
        //            {
        //                string strContent = File.ReadAllText(strFilePath);
        //                strContent = Regex.Replace(strContent, "跟踪精度:" + stsx, "跟踪精度:" + txtXx.ToString());
        //                File.WriteAllText(strFilePath, strContent);
        //                csName.pl[16, 1] = txtXx.ToString();
        //            }
        //            ysJd = 0;//精度置零
        //            richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss") + ">跟踪精度已修改为:" + csName.pl[16, 1] + "!\r\n");
        //            csName.rzArray.Enqueue(DateTime.Now.ToString("HH:mm:ss") + ">跟踪精度已修改为:" + csName.pl[16, 1] + "!"); 
        //        }
        //    }
        //    catch
        //    {
        //        tabControl1.SelectedIndex = 0;
        //        gzJd.Text = csName.pl[16, 1];
        //        richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss") + ">跟踪精度修改失败！\r\n");
        //        csName.rzArray.Enqueue(DateTime.Now.ToString("HH:mm:ss") + ">跟踪精度修改失败！"); 
        //    }
        //}

        //时间补偿变更
        private void 时间补偿toolStripMenuItem10_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string stsx = csName.pl[17, 1];
                int txtXx = 0;
                if (csName.pl[17, 1] != timeBc.Text)
                {
                    txtXx = Convert.ToInt32(timeBc.Text);
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "程跟时间补偿:" + stsx, "程跟时间补偿:" + txtXx.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[17, 1] = txtXx.ToString();
                    }
                    WriteInfo("程跟时间补偿值已修改为:" + csName.pl[17, 1] + "ms!");
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                timeBc.Text = csName.pl[17, 1];
                WriteInfo("程跟时间补偿值修改失败！");
            }
        }
        //时间间隔变更
        private void 时间补偿toolStripMenuItem2_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string stsx = csName.pl[18, 1];
                int txtXx = 0;
                if (csName.pl[18, 1] != timeSy.Text)
                {

                    txtXx = Convert.ToInt32(timeSy.Text);
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "数引时间补偿:" + stsx, "数引时间补偿:" + txtXx.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[18, 1] = txtXx.ToString();
                    }
                    Info.Text = Info.Text + DateTime.Now.ToString("HH:mm:ss") + ">数引时间补偿已修改为:" + csName.pl[18, 1] + "ms!\r\n";
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                timeSy.Text = csName.pl[18, 1];
                WriteInfo("数引时间补偿值修改失败！");
            }
        }
        //扫描设置变更
        private void 扫描设置toolStripMenuItem13_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string f1 = csName.pl[12, 1];
                string f2 = csName.pl[13, 1];
                string f3 = csName.pl[14, 1];
                string f4 = csName.pl[15, 1];
                string f6 = csName.pl[47, 1];
                string f7 = csName.pl[48, 1];
                string f5 = csName.pl[28, 1];
                double hf1 = 0;
                double hf2 = 0;
                double hf3 = 0;
                double hf4 = 0;
                double hf5 = 0;
                double hf6 = 0;
                double hf7 = 0;
                if (csName.pl[12, 1] != fwSd.Text || csName.pl[13, 1] != fySd.Text || csName.pl[14, 1] != fwFw.Text
                    || csName.pl[15, 1] != fyFw.Text || csName.pl[28, 1] != cmxSmSc.Text ||
                   csName.pl[47, 1] != toolStripTextBox5.Text || csName.pl[48, 1] != toolStripTextBox6.Text)
                {
                    hf1 = Convert.ToDouble(fwSd.Text);
                    hf2 = Convert.ToDouble(fySd.Text);
                    hf3 = Convert.ToDouble(fwFw.Text);
                    hf4 = Convert.ToDouble(fyFw.Text);
                    hf5 = Convert.ToInt32(cmxSmSc.Text);
                    hf6 = Convert.ToDouble(toolStripTextBox5.Text);
                    hf7 = Convert.ToDouble(toolStripTextBox6.Text);
                    if (hf1 > 0 && hf2 > 0 && hf3 > 0 && hf4 > 0 && hf5 > 0 && hf6 > 0 && hf7 > 0)
                    {
                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "方位扫描速度:" + f1 + "\r\n俯仰扫描速度:" + f2 + "\r\n方位扫描范围:" + f3 + "\r\n俯仰扫描范围:" + f4,
                                "方位扫描速度:" + hf1.ToString() + "\r\n俯仰扫描速度:" + hf2.ToString() + "\r\n方位扫描范围:" + hf3.ToString() + "\r\n俯仰扫描范围:" + hf4.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            strContent = Regex.Replace(strContent, "扫描次数:" + f5,
                               "扫描次数:" + hf5.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            strContent = Regex.Replace(strContent, "极化扫描范围:" + f6 + "\r\n极化扫描速度:" + f7,
                             "极化扫描范围:" + hf6.ToString() + "\r\n极化扫描速度:" + hf7.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[12, 1] = hf1.ToString();
                            csName.pl[13, 1] = hf2.ToString();
                            csName.pl[14, 1] = hf3.ToString();
                            csName.pl[15, 1] = hf4.ToString();
                            csName.pl[28, 1] = hf5.ToString();
                            csName.pl[47, 1] = hf6.ToString();
                            csName.pl[48, 1] = hf7.ToString();

                        }
                        WriteInfo("扫描设置已修改,依次为:" + hf1.ToString() + "；" + hf2.ToString() + "；" + hf3.ToString() + "；" + hf4.ToString() + "；" + hf6.ToString() + "；" + hf7.ToString() + "；" + hf5.ToString() + "!");
                    }
                    else
                    {
                        tabControl1.SelectedIndex = 0;
                        fwSd.Text = csName.pl[12, 1];//X轴扫描速度值
                        fySd.Text = csName.pl[13, 1];//Y轴扫描速度值
                        fwFw.Text = csName.pl[14, 1];//X轴扫描范围
                        fyFw.Text = csName.pl[15, 1];//Y轴扫描范围
                        cmxSmSc.Text = csName.pl[28, 1];//扫描时长
                        toolStripTextBox5.Text = csName.pl[47, 1];
                        toolStripTextBox6.Text = csName.pl[48, 1];
                        WriteInfo("扫描设置修改失败！");
                    }
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                fwSd.Text = csName.pl[12, 1];//X轴扫描速度值
                fySd.Text = csName.pl[13, 1];//Y轴扫描速度值
                fwFw.Text = csName.pl[14, 1];//X轴扫描范围
                fyFw.Text = csName.pl[15, 1];//Y轴扫描范围
                cmxSmSc.Text = csName.pl[28, 1];//扫描时长
                WriteInfo("扫描设置修改失败！");
            }
        }
        //天线命令速度变更
        private void 天线命令速度ToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string f1 = csName.pl[10, 1];
                string f2 = csName.pl[11, 1];
                string f3 = csName.pl[40, 1];
                double hf1 = 0;
                double hf2 = 0;
                double hf3 = 0;
                if (csName.pl[10, 1] != fwMlSd.Text || csName.pl[11, 1] != fyMlSd.Text || csName.pl[40, 1] != toolStripTextBox4.Text)
                {
                    hf1 = Convert.ToDouble(fwMlSd.Text);
                    hf2 = Convert.ToDouble(fyMlSd.Text);
                    hf3 = Convert.ToDouble(toolStripTextBox4.Text);
                    if (hf1 > 0 && hf1 <= 30 && hf2 <= 30 && hf2 > 0 && hf3 <= 30 && hf3 > 0)
                    {
                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "方位命令速度:" + f1 + "\r\n俯仰命令速度:" + f2,
                                "方位命令速度:" + hf1.ToString() + "\r\n俯仰命令速度:" + hf2.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            strContent = Regex.Replace(strContent, "极化命令速度:" + f3,
                               "极化命令速度:" + hf3.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[10, 1] = hf1.ToString();
                            csName.pl[11, 1] = hf2.ToString();
                            csName.pl[40, 1] = hf3.ToString();
                        }
                        WriteInfo("天线命令速度已修改,依次为:" + hf1.ToString() + "；" + hf2.ToString() + "；" + hf3.ToString() + "!");
                    }
                    else
                    {
                        tabControl1.SelectedIndex = 0;
                        fwMlSd.Text = csName.pl[10, 1];//X轴命令速度值
                        fyMlSd.Text = csName.pl[11, 1];//Y轴命令速度值
                        toolStripTextBox4.Text = csName.pl[40, 1];//Y轴命令速度值
                        WriteInfo("天线命令速度修改失败,输入值不合法！");
                    }
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                fwMlSd.Text = csName.pl[10, 1];//X轴命令速度值
                fyMlSd.Text = csName.pl[11, 1];//Y轴命令速度值
                WriteInfo("天线命令速度修改失败！");
            }
        }
        //限位变更
        private void 限位设置toolStripMenuItem7_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string f1 = csName.pl[19, 1];
                string f2 = csName.pl[20, 1];
                string f3 = csName.pl[21, 1];
                string f4 = csName.pl[22, 1];
                string f5 = csName.pl[41, 1];
                string f6 = csName.pl[42, 1];

                if (csName.pl[19, 1] != fwSx.Text || csName.pl[20, 1] != fwNx.Text || csName.pl[21, 1] != fySx.Text || csName.pl[22, 1] != fyXx.Text || csName.pl[41, 1] != toolStripTextBox2.Text || csName.pl[42, 1] != toolStripTextBox3.Text)
                {
                    double hf1 = Convert.ToDouble(fwSx.Text);
                    double hf2 = Convert.ToDouble(fwNx.Text);
                    double hf3 = Convert.ToDouble(fySx.Text);
                    double hf4 = Convert.ToDouble(fyXx.Text);
                    double hf5 = Convert.ToDouble(toolStripTextBox2.Text);
                    double hf6 = Convert.ToDouble(toolStripTextBox3.Text);
                    if (hf1 > hf2 && hf3 > hf4 && hf5 > hf6)
                    {
                        if(hf1<=360&& hf2>=0&& hf3 <= 90 && hf4 >= 0 && hf5 <= 90 && hf6 >= -90)
                        {
                            string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                            if (File.Exists(strFilePath))
                            {
                                string strContent = File.ReadAllText(strFilePath);
                                strContent = Regex.Replace(strContent, "方位顺限:" + f1 + "\r\n方位逆限:" + f2 + "\r\n俯仰上限:" + f3 + "\r\n俯仰下限:" + f4,
                                    "方位顺限:" + hf1.ToString() + "\r\n方位逆限:" + hf2.ToString() + "\r\n俯仰上限:" + hf3.ToString() + "\r\n俯仰下限:" + hf4.ToString());
                                File.WriteAllText(strFilePath, strContent);
                                strContent = Regex.Replace(strContent, "极化顺限:" + f5 + "\r\n极化逆限:" + f6,
                                    "极化顺限:" + hf5.ToString() + "\r\n极化逆限:" + hf6.ToString());
                                File.WriteAllText(strFilePath, strContent);
                                csName.pl[19, 1] = hf1.ToString();
                                csName.pl[20, 1] = hf2.ToString();
                                csName.pl[21, 1] = hf3.ToString();
                                csName.pl[22, 1] = hf4.ToString();
                                csName.pl[41, 1] = hf5.ToString();
                                csName.pl[42, 1] = hf6.ToString();
                                AxisControl.setRLimit(0, Convert.ToDouble(csName.pl[19, 1]), Convert.ToDouble(csName.pl[20, 1]));
                                AxisControl.setRLimit(1, Convert.ToDouble(csName.pl[21, 1]), Convert.ToDouble(csName.pl[22, 1]));
                                AxisControl.setRLimit(2, Convert.ToDouble(csName.pl[41, 1]), Convert.ToDouble(csName.pl[42, 1]));
                            }
                            WriteInfo("天线限位已修改,依此为:" + hf1.ToString() + "；" + hf2.ToString() + "；" + hf3.ToString() + "；" + hf4.ToString() + "；" + hf5.ToString() + "；" + hf6.ToString() + "!");
                        }
                        else
                        {
                            tabControl1.SelectedIndex = 0;
                            fwSx.Text = csName.pl[19, 1];//X轴顺限
                            fwNx.Text = csName.pl[20, 1];//X轴逆限
                            fySx.Text = csName.pl[21, 1];//Y轴上限
                            fyXx.Text = csName.pl[22, 1];//Y轴下限
                            toolStripTextBox2.Text = csName.pl[41, 1];//Y轴上限
                            toolStripTextBox3.Text = csName.pl[42, 1];//Y轴下限
                            WriteInfo("天线限位修改失败,限位值设置超出范围，请重设置！");
                        }
                    }
                    else
                    {
                        tabControl1.SelectedIndex = 0;
                        fwSx.Text = csName.pl[19, 1];//X轴顺限
                        fwNx.Text = csName.pl[20, 1];//X轴逆限
                        fySx.Text = csName.pl[21, 1];//Y轴上限
                        fyXx.Text = csName.pl[22, 1];//Y轴下限
                        toolStripTextBox2.Text = csName.pl[41, 1];//Y轴上限
                        toolStripTextBox3.Text = csName.pl[42, 1];//Y轴下限
                        WriteInfo("天线限位修改失败,顺限位必须大于逆限位，请重设置！");
                    }
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                fwSx.Text = csName.pl[19, 1];//X轴顺限
                fwNx.Text = csName.pl[20, 1];//X轴逆限
                fySx.Text = csName.pl[21, 1];//Y轴上限
                fyXx.Text = csName.pl[22, 1];//Y轴下限
                WriteInfo("天线限位修改失败！");
            }
        }
        //通道门限变更
        private void 通道门限ToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string stsx = csName.pl[6, 1];
                string stxx = csName.pl[7, 1];
                double txtSx = 10.000;
                double txtXx = 0.000;
                if (csName.pl[6, 1] != mxTop.Text || csName.pl[7, 1] != mxBottom.Text)
                {
                    txtSx = Convert.ToDouble(mxTop.Text);
                    txtXx = Convert.ToDouble(mxBottom.Text);
                    if (txtSx > txtXx && txtSx > 0 && txtSx <= 10 && txtXx >= 0 && txtXx < 10)
                    {
                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "通道上限:" + stsx + "\r\n通道下限:" + stxx, "通道上限:" + txtSx.ToString() + "\r\n通道下限:" + txtXx.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[6, 1] = txtSx.ToString();
                            csName.pl[7, 1] = txtXx.ToString();
                        }
                        WriteInfo("通道门限已修改,上限为：" + csName.pl[6, 1] + ",下限为：" + csName.pl[7, 1] + "!");
                    }
                    else
                    {
                        tabControl1.SelectedIndex = 0;
                        mxTop.Text = csName.pl[6, 1];
                        mxBottom.Text = csName.pl[7, 1];
                        WriteInfo("通道门限修改失败，输入值不合法！");

                    }
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                mxTop.Text = csName.pl[6, 1];
                mxBottom.Text = csName.pl[7, 1];
                WriteInfo("通道门限修改失败！");
            }
        }
        //测角日志变更
        private void 测角日志toolStripMenuItem4_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string f1 = csName.pl[25, 1];
                string f2 = csName.pl[26, 1];
                string f3 = csName.pl[27, 1];
                string hf1 = "";
                string hf2 = "";
                int hf3 = 0;
                if (csName.pl[25, 1] != rzLj.Text)
                {
                    hf1 = rzLj.Text;
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "日志路径:" + f1.Replace("\\", "\\\\"), "日志路径:" + hf1.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[25, 1] = hf1.ToString();
                    }
                    WriteInfo("系统日志路径修改为:" + csName.pl[25, 1] + "!");
                }
                if (csName.pl[26, 1] != sjLj.Text)
                {
                    hf2 = sjLj.Text;
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "数据路径:" + f2.Replace("\\", "\\\\"), "数据路径:" + hf2.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[26, 1] = hf2.ToString();
                    }
                    WriteInfo("卫星跟踪数据存储路径修改为:" + csName.pl[26, 1] + "!");
                }

                if (csName.pl[27, 1] != gzJlJg.Text)
                {
                    hf3 = Convert.ToInt32(gzJlJg.Text);
                    if (hf3 > 0)
                    {
                        string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                        if (File.Exists(strFilePath))
                        {
                            string strContent = File.ReadAllText(strFilePath);
                            strContent = Regex.Replace(strContent, "记录间隔:" + f3, "记录间隔:" + hf3.ToString());
                            File.WriteAllText(strFilePath, strContent);
                            csName.pl[27, 1] = hf3.ToString();
                        }
                        WriteInfo("跟踪数据记录间隔修改为:" + csName.pl[27, 1] + "ms!");
                    }
                    else
                    {
                        tabControl1.SelectedIndex = 0;
                        WriteInfo("跟踪数据记录修改失败!");
                    }
                }

            }
            catch
            {
                rzLj.Text = csName.pl[25, 1];//日志路径
                sjLj.Text = csName.pl[26, 1];//数据路径
                gzJlJg.Text = csName.pl[27, 1];//记录间隔
                tabControl1.SelectedIndex = 0;
                WriteInfo("系统日志记录及卫星跟踪数据存盘修改失败！");
            }
        }
        private void rzLj_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                rzLj.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void sjLj_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                sjLj.Text = folderBrowserDialog1.SelectedPath;
            }
        }
        //角度修正值
        private void 角度修正toolStripMenuItem6_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                string stfw = csName.pl[8, 1];
                string stfy = csName.pl[9, 1];
                string stP = csName.pl[43, 1];
                double txtfw = 0.000;
                double txtfy = 0.000;
                double txtP = 0.000;
                if (csName.pl[8, 1] != fwXz.Text || csName.pl[9, 1] != fyXz.Text || csName.pl[43, 1] != toolStripTextBox1.Text)
                {

                    txtfw = Convert.ToDouble(fwXz.Text);
                    txtfy = Convert.ToDouble(fyXz.Text);
                    txtP = Convert.ToDouble(toolStripTextBox1.Text);
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "方位修正:" + stfw + "\r\n俯仰修正:" + stfy, "方位修正:" + txtfw.ToString() + "\r\n俯仰修正:" + txtfy.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        strContent = Regex.Replace(strContent, "极化修正:" + stP, "极化修正:" + txtP.ToString());
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[8, 1] = txtfw.ToString();
                        csName.pl[9, 1] = txtfy.ToString();
                        csName.pl[43, 1] = txtP.ToString();
                        AxisControl.setCorrected(0, Convert.ToDouble(csName.pl[8, 1]));
                        AxisControl.setCorrected(1, Convert.ToDouble(csName.pl[9, 1]));
                        AxisControl.setCorrected(2, Convert.ToDouble(csName.pl[43, 1]));
                    }
                    WriteInfo("天线角度修正值已改,依次为：" + csName.pl[8, 1] + "；" + csName.pl[9, 1] + "；" + csName.pl[43, 1] + "!");
                }
            }
            catch
            {
                tabControl1.SelectedIndex = 0;
                fwXz.Text = csName.pl[8, 1];//X轴修正值
                fyXz.Text = csName.pl[9, 1];//Y轴修正值
                toolStripTextBox1.Text = csName.pl[43, 1];//Y轴修正值
                WriteInfo("角度修正失败！");
            }
        }
        //位置变更
        private void 位置设置ToolStripMenuItem_DropDownClosed(object sender, EventArgs e)
        {
            string[] data = csName.pl[0, 1].Split(',');
            if (data[0] != wzWd.Text || data[2] != wzJd.Text || data[4] != wzHbg.Text)
            {
                try
                {
                    double txtWd = Convert.ToDouble(wzWd.Text);
                    double txtJd = Convert.ToDouble(wzJd.Text);
                    double txtHbg = Convert.ToDouble(wzHbg.Text);
                    string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                    if (File.Exists(strFilePath))
                    {
                        string strContent = File.ReadAllText(strFilePath);
                        strContent = Regex.Replace(strContent, "天线位置:" + data[0] + ",N," + data[2] + ",E," + data[4], "天线位置:" + wzWd.Text + ",N," + wzJd.Text + ",E," + wzHbg.Text);
                        File.WriteAllText(strFilePath, strContent);
                        csName.pl[0, 1] = wzWd.Text + ",N," + wzJd.Text + ",E," + wzHbg.Text;
                    }
                    WriteInfo("位置信息已修改,北纬为：" + wzWd.Text + ",东经为：" + wzJd.Text + ",海拔高为：" + wzHbg.Text + "km!");
                    tongbu = true;
                    if (csName.Mode == 1)
                    {
                        if (tbclfs.Text == "单星任务")
                        {
                            oneGraphThread = new Thread(oneGraph);
                            oneGraphThread.Start();
                        }
                        else
                        {
                            mostGraphThread = new Thread(mostGraph);
                            mostGraphThread.Start();
                        }
                    }
                }
                catch
                {
                    tabControl1.SelectedIndex = 0;
                    wzWd.Text = data[0];
                    wzJd.Text = data[2];
                    wzHbg.Text = data[4];
                    WriteInfo("位置信息修改失败！");
                }
            }
        }
        //系统性能参数
        private void toolStripMenuItem1_DropDownClosed(object sender, EventArgs e)
        {
            string f1 = csName.pl[29, 1];
            string f2 = csName.pl[30, 1];
            string f3 = csName.pl[31, 1];
            string f4 = csName.pl[32, 1];
            string f5 = csName.pl[33, 1];
            string f6 = csName.pl[34, 1];
            string f7 = csName.pl[35, 1];
            string hf1;
            string hf2;
            string hf3;
            string hf4;
            string hf5;
            string hf6;
            string hf7;
            if (fwCg.Text != csName.pl[29, 1] || fyCg.Text != csName.pl[30, 1] || fwSy.Text != csName.pl[31, 1] || fySy.Text != csName.pl[32, 1] || fwSf.Text != csName.pl[33, 1] || fySf.Text != csName.pl[34, 1] || dwJd.Text != csName.pl[35, 1])
            {
                hf1 = fwCg.Text;
                hf2 = fyCg.Text;
                hf3 = fwSy.Text;
                hf4 = fySy.Text;
                hf5 = fwSf.Text;
                hf6 = fySf.Text;
                hf7 = dwJd.Text;
                string strFilePath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Environment.CurrentDirectory + "\\text\\deploy.ini");
                if (File.Exists(strFilePath))
                {
                    string strContent = File.ReadAllText(strFilePath);
                    strContent = Regex.Replace(strContent, "程跟X轴PID:" + f1 + "\r\n程跟Y轴PID:" + f2 + "\r\n数引X轴PID:" + f3 + "\r\n数引Y轴PID:" + f4 + "\r\n伺服X轴PID:" + f5 + "\r\n伺服Y轴PID:" + f6 + "\r\n定位精度:" + f7,
                        "程跟X轴PID:" + hf1.ToString() + "\r\n程跟Y轴PID:" + hf2.ToString() + "\r\n数引X轴PID:" + hf3.ToString() + "\r\n数引Y轴PID:" + hf4.ToString() + "\r\n伺服X轴PID:" + hf5.ToString() + "\r\n伺服Y轴PID:" + hf6.ToString() + "\r\n定位精度:" + hf7.ToString());
                    File.WriteAllText(strFilePath, strContent);
                    csName.pl[29, 1] = hf1.ToString();
                    csName.pl[30, 1] = hf2.ToString();
                    csName.pl[31, 1] = hf3.ToString();
                    csName.pl[32, 1] = hf4.ToString();
                    csName.pl[33, 1] = hf5.ToString();
                    csName.pl[34, 1] = hf6.ToString();
                    csName.pl[35, 1] = hf7.ToString();
                    csName.dwJd = csName.pl[35, 1].Split(',');
                }
                WriteInfo("系统性能参数修改成功!");
            }
        }
        //预置位置参数修改
        private void cbYzwz_DropDownClosed(object sender, EventArgs e)
        {
            int z = cbYzwz.Items.IndexOf(cbYzwz.Text);
            string[] yzw = new string[4];
            yzw = csName.yzWz[z].Split(',');
            txtFwml.Text = yzw[1];
            txtFyml.Text = yzw[2];
        }
        #endregion

        #region 调速，运动键盘响应事件
        //屏蔽方向键对其他控件的响应
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.W)
            {
                return false;
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
        }
        //键盘按下事件

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (tbBX.SelectedIndex == 0 && txtFwml.Focused == false && txtFyml.Focused == false && tbBX.Focused == false)
            {

                switch (e.KeyCode)
                {

                    case Keys.Up:
                        if (btnTop.Enabled == true && btnTop.BackColor == Color.Silver) { btnTop_MouseDown(null, null); }
                        break;
                    case Keys.Down:
                        if (btnBottom.Enabled == true && btnBottom.BackColor == Color.Silver) { btnBottom_MouseDown(null, null); }
                        break;
                    case Keys.Left:
                        if (btnLeft.Enabled == true && btnLeft.BackColor == Color.Silver) { btnLeft_MouseDown(null, null); }
                        break;
                    case Keys.Right:
                        if (btnRight.Enabled == true && btnRight.BackColor == Color.Silver) { btnRight_MouseDown(null, null); }
                        break;
                    //
                    case Keys.W:
                        if (fyAdd.Enabled == true && fyAdd.BackColor == Color.Silver) { fyAdd_MouseDown(null, null); }
                        break;
                    case Keys.S:
                        if (fySubtract.Enabled == true && fySubtract.BackColor == Color.Silver) { fySubtract_MouseDown(null, null); }
                        break;
                    case Keys.A:
                        if (fwAdd.Enabled == true && fwAdd.BackColor == Color.Silver) { button4_MouseDown(null, null); }
                        break;
                    case Keys.D:
                        if (fwSubtract.Enabled == true && fwSubtract.BackColor == Color.Silver) { fwSubtract_MouseDown(null, null); }
                        break;
                }
            }
        }
        //键盘释放事件
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (tbBX.SelectedIndex == 0 && txtFwml.Focused == false && txtFyml.Focused == false && tbBX.Focused == false)
            {
                switch (e.KeyCode)
                {
                    case Keys.Up:
                        if (btnTop.Enabled == true) { btnTop_MouseUp(null, null); }
                        break;
                    case Keys.Down:
                        if (btnBottom.Enabled == true) { btnBottom_MouseUp(null, null); }
                        break;
                    case Keys.Left:
                        if (btnLeft.Enabled == true) { btnLeft_MouseUp(null, null); }
                        break;
                    case Keys.Right:
                        if (btnRight.Enabled == true) { btnRight_MouseUp(null, null); }
                        break;
                    //
                    case Keys.W:
                        if (fyAdd.Enabled == true) { fyAdd_MouseUp(null, null); }
                        break;
                    case Keys.S:
                        if (fySubtract.Enabled == true) { fySubtract_MouseUp(null, null); }
                        break;
                    case Keys.A:
                        if (fwAdd.Enabled == true) { button4_MouseUp(null, null); }
                        break;
                    case Keys.D:
                        if (fwSubtract.Enabled == true) { fwSubtract_MouseUp(null, null); }
                        break;
                }
            }
        }
        //左动
        bool unLockfwl = true;
        double leftJd = 0;
        private void btnLeft_MouseDown(object sender, MouseEventArgs e)
        {
            if (btnFwkj.BackColor != Color.Lime) return;
            csName.fwSk = true;
            btnLeft.BackColor = Color.Lime;
            label25.Text = "-";
            if (csName.boolAGC == false)
            {
                unLockfwl = false;
            }
            leftJd = csName.fWjD;
            Rotation.GetInstancce().IsPauseAzTask = true;//暂停自动执行
            Rotation.GetInstancce().Turn(TurnDirect.Left, csName.sdzlFw);
        }
        private void btnLeft_MouseUp(object sender, MouseEventArgs e)
        {
            if (btnFwkj.BackColor != Color.Lime) return;
            Rotation.GetInstancce().Turn(TurnDirect.StopAz, 0);
            Rotation.GetInstancce().IsPauseAzTask = false;//重启自动执行
            csName.fwSk = false;
            btnLeft.BackColor = Color.Silver;
            label25.Text = "";
            if (unLockfwl == false && btnZdzx.BackColor == Color.Lime && lblX_LOCK.ForeColor == Color.Lime)//角度修正
            {
                if (csName.pl[23, 1] == "程序跟踪")
                {
                    double a = csName.fWYsjD - csName.fWjD;
                    fwXz.Text = a.ToString();
                }
                else
                {
                    double a = csName.fWjyjD - csName.fWjD;
                    fwXz.Text = a.ToString();
                }
                角度修正toolStripMenuItem6_DropDownClosed(null, null);
                unLockfwl = true;
            }
        }

        //右动
        bool unLockfwr = true;
        double rightJd = 0;
        private void btnRight_MouseDown(object sender, MouseEventArgs e)
        {
            if (btnFwkj.BackColor != Color.Lime) return;
            csName.fwSk = true;
            btnRight.BackColor = Color.Lime;
            label25.Text = "+";
            if (csName.boolAGC == false)
            {
                unLockfwr = false;
            }
            rightJd = csName.fWjD;
            Rotation.GetInstancce().IsPauseAzTask = true;
            Rotation.GetInstancce().Turn(TurnDirect.Right, csName.sdzlFw);
        }
        private void btnRight_MouseUp(object sender, MouseEventArgs e)
        {
            if (btnFwkj.BackColor != Color.Lime) return;
            Rotation.GetInstancce().Turn(TurnDirect.StopAz, 0);
            Rotation.GetInstancce().IsPauseAzTask = false;
            csName.fwSk = false;
            btnRight.BackColor = Color.Silver;
            label25.Text = "";
            if (unLockfwr == false && btnZdzx.BackColor == Color.Lime && lblX_LOCK.ForeColor == Color.Lime)//角度修正
            {
                if (csName.pl[23, 1] == "程序跟踪")
                {
                    double a = csName.fWYsjD - (csName.fWjD);
                    fwXz.Text = a.ToString();
                }
                else
                {
                    double a = csName.fWjyjD - (csName.fWjD);
                    fwXz.Text = a.ToString();
                }
                unLockfwr = true;
                角度修正toolStripMenuItem6_DropDownClosed(null, null);
            }
        }

        //上动
        bool unLockfwt = true;
        double topJd = 0;
        private void btnTop_MouseDown(object sender, MouseEventArgs e)
        {
            if (btnFykj.BackColor != Color.Lime) return;
            csName.fySk = true;
            btnTop.BackColor = Color.Lime;
            label26.Text = "+";
            if (csName.boolAGC == false)
            {
                unLockfwt = false;
            }
            topJd = csName.fYjD;
            Rotation.GetInstancce().IsPauseElTask = true;
            Rotation.GetInstancce().Turn(TurnDirect.Up, csName.sdzlFy);
        }
        private void btnTop_MouseUp(object sender, MouseEventArgs e)
        {
            if (btnFykj.BackColor != Color.Lime) return;
            Rotation.GetInstancce().Turn(TurnDirect.StopEl, 0);
            Rotation.GetInstancce().IsPauseElTask = false;
            csName.fySk = false;
            btnTop.BackColor = Color.Silver;
            label26.Text = "";
            if (unLockfwt == false && btnZdzx.BackColor == Color.Lime && lblX_LOCK.ForeColor == Color.Lime)//角度修正
            {
                if (csName.pl[23, 1] == "程序跟踪")
                {
                    double a = csName.fYYsjD - (csName.fYjD);
                    fyXz.Text = a.ToString();
                }
                else
                {
                    double a = csName.fYjyjD - (csName.fYjD);
                    fyXz.Text = a.ToString();
                }
                unLockfwt = true;
                角度修正toolStripMenuItem6_DropDownClosed(null, null);

            }
        }

        //下动
        bool unLockfwb = true;
        //double bottomJd = 0;
        private void btnBottom_MouseDown(object sender, MouseEventArgs e)
        {
            if (btnFykj.BackColor != Color.Lime) return;
            csName.fySk = true;
            btnBottom.BackColor = Color.Lime;
            label26.Text = "-";
            if (csName.boolAGC == false)
            {
                unLockfwb = false;
            }
            //bottomJd = csName.fYjD;
            //BGraphThread = new Thread(BGraph);
            //BGraphThread.Priority = ThreadPriority.Highest;
            //BGraphThread.Start();
            Rotation.GetInstancce().IsPauseElTask = true;
            Rotation.GetInstancce().Turn(TurnDirect.Down, csName.sdzlFy);
            // Console.WriteLine("当前Y轴速度：" + csName.sdzlFy);
        }

        private void btnBottom_MouseUp(object sender, MouseEventArgs e)
        {
            if (btnFykj.BackColor != Color.Lime) return;
            Rotation.GetInstancce().Turn(TurnDirect.StopEl, 0);
            Rotation.GetInstancce().IsPauseElTask = false;
            csName.fySk = false;
            btnBottom.BackColor = Color.Silver;
            label26.Text = "";
            if (unLockfwb == false && btnZdzx.BackColor == Color.Lime && lblX_LOCK.ForeColor == Color.Lime)//角度修正
            {
                if (csName.pl[23, 1] == "程序跟踪")
                {
                    double a = csName.fYYsjD - (csName.fYjD);
                    fyXz.Text = a.ToString();
                }
                else
                {
                    double a = csName.fYjyjD - (csName.fYjD);
                    fyXz.Text = a.ToString();
                }
                unLockfwb = true;
                角度修正toolStripMenuItem6_DropDownClosed(null, null);
            }
        }
        //声明下动工作线程
        //+A
        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            fwAdd.BackColor = Color.Lime;
            AdGraphThread = new Thread(AdGraph);
            AdGraphThread.Start();
            lblFwsdzl.ForeColor = Color.Green;
        }

        private void button4_MouseUp(object sender, MouseEventArgs e)
        {
            fwAdd.BackColor = Color.Silver;
            lblFwsdzl.ForeColor = Color.Black;
        }
        //声明水平速度调整工作线程
        Thread AdGraphThread;
        private void AdGraph()
        {
            for (int i = 0; i < 10; i++)
            {
                if (fwAdd.BackColor != Color.Lime && fwSubtract.BackColor != Color.Lime)
                { break; }
                double bc = 0;
                if (rbJtbj1.Checked == true) { bc = 0.001; }
                else if (rbJtbj2.Checked == true) { bc = 0.01; }
                else { bc = 0.06; }
                this.Invoke((EventHandler)(delegate
                    {
                        if (Convert.ToDouble(lblFwsdzl.Text) >= 0 && Convert.ToDouble(lblFwsdzl.Text) <= 20)
                        {
                            if (fwAdd.BackColor == Color.Lime)
                            {
                                lblFwsdzl.Text = (Convert.ToDouble(lblFwsdzl.Text) + bc).ToString("0.000");
                                if (Convert.ToDouble(lblFwsdzl.Text) > 20) { lblFwsdzl.Text = "20.000"; }
                                csName.sdzlFw = Convert.ToDouble(lblFwsdzl.Text);
                            }
                            else if (fwSubtract.BackColor == Color.Lime)
                            {
                                lblFwsdzl.Text = (Convert.ToDouble(lblFwsdzl.Text) - bc).ToString("0.000");
                                if (Convert.ToDouble(lblFwsdzl.Text) < 0) { lblFwsdzl.Text = "0.000"; }
                                csName.sdzlFw = Convert.ToDouble(lblFwsdzl.Text);
                            }
                        }
                    }
                 ));
                i = 0;
                Thread.Sleep(100);
            }
        }
        //-D
        private void fwSubtract_MouseDown(object sender, MouseEventArgs e)
        {
            fwSubtract.BackColor = Color.Lime;
            AdGraphThread = new Thread(AdGraph);
            AdGraphThread.Start();
            lblFwsdzl.ForeColor = Color.Green;
        }
        private void fwSubtract_MouseUp(object sender, MouseEventArgs e)
        {
            fwSubtract.BackColor = Color.Silver;
            lblFwsdzl.ForeColor = Color.Black;
        }
        //+W
        private void fyAdd_MouseDown(object sender, MouseEventArgs e)
        {
            fyAdd.BackColor = Color.Lime;
            WsGraphThread = new Thread(WsGraph);
            WsGraphThread.Start();
            lblFysdzl.ForeColor = Color.Green;
        }
        private void fyAdd_MouseUp(object sender, MouseEventArgs e)
        {
            fyAdd.BackColor = Color.Silver;
            lblFysdzl.ForeColor = Color.Black; ;
        }
        //声明Y轴速度调整工作线程
        Thread WsGraphThread;
        private void WsGraph()
        {
            for (int i = 0; i < 10; i++)
            {
                if (fyAdd.BackColor != Color.Lime && fySubtract.BackColor != Color.Lime)
                { break; }
                double bc = 0;
                if (rbJtbj1.Checked == true) { bc = 0.001; }
                else if (rbJtbj2.Checked == true) { bc = 0.01; }
                else { bc = 0.06; }
                this.Invoke((EventHandler)(delegate
                {
                    if (Convert.ToDouble(lblFysdzl.Text) >= 0 && Convert.ToDouble(lblFysdzl.Text) <= 10)
                    {
                        if (fyAdd.BackColor == Color.Lime)
                        {
                            lblFysdzl.Text = (Convert.ToDouble(lblFysdzl.Text) + bc).ToString("0.000");
                            if (Convert.ToDouble(lblFysdzl.Text) > 10) { lblFysdzl.Text = "10.000"; }
                            csName.sdzlFy = Convert.ToDouble(lblFysdzl.Text);
                        }
                        else if (fySubtract.BackColor == Color.Lime)
                        {
                            lblFysdzl.Text = (Convert.ToDouble(lblFysdzl.Text) - bc).ToString("0.000");
                            if (Convert.ToDouble(lblFysdzl.Text) < 0) { lblFysdzl.Text = "0.000"; }
                            csName.sdzlFy = Convert.ToDouble(lblFysdzl.Text);
                        }
                    }
                }
             ));
                i = 0;
                Thread.Sleep(100);
            }
        }
        //-S
        private void fySubtract_MouseDown(object sender, MouseEventArgs e)
        {
            fySubtract.BackColor = Color.Lime;
            WsGraphThread = new Thread(WsGraph);
            WsGraphThread.Start();
            lblFysdzl.ForeColor = Color.Green;
        }

        private void fySubtract_MouseUp(object sender, MouseEventArgs e)
        {
            fySubtract.BackColor = Color.Silver;
            lblFysdzl.ForeColor = Color.Black;
        }
        #endregion
        #region 远程事件

        Thread threadWatch = null; //负责监听客户端的线程
        Socket socketWatch = null;  //负责监听客户端的套接字   
        public void ServerCon()
        {
            try
            {

                boolServerSendMsg = true;
                //定义一个套接字用于监听客户端发来的信息  包含3个参数(IP4寻址协议,流式连接,TCP协议)
                socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socketWatch.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                //服务端发送信息 需要1个IP地址和端口号
                IPAddress ipaddress = IPAddress.Parse(csName.pl[4, 1].Trim()); //获取IP地址
                //将IP地址和端口号绑定到网络节点endpoint上 
                IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(csName.pl[5, 1].Trim())); //获取端口号
                //监听绑定的网络节点
                socketWatch.Bind(endpoint);
                //将套接字的监听队列长度限制为10
                socketWatch.Listen(100);
                //创建一个监听线程 
                threadWatch = new Thread(WatchConnecting) { IsBackground = true };
                //启动线程
                threadWatch.Start();
            }
            catch
            {
                this.Invoke((EventHandler)(delegate
                {
                    WriteInfo("远程监控服务启动失败！");
                }));
            }
        }
        //创建一个负责和客户端通信的套接字 
        Socket socConnection = null;
        // 监听客户端发来的请求
        private void WatchConnecting()
        {
            while (true)  //持续不断监听客户端发来的请求
            {

                try
                {
                    socConnection = socketWatch.Accept();
                    //创建一个通信线程 
                    ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsg);
                    Thread thr = new Thread(pts) { IsBackground = true };
                    //启动线程
                    thr.Start(socConnection);
                    socketWatch.Close();
                    break;
                }
                catch
                {
                    if (csName.pl[24, 1] == "否") { break; }
                }
            }
        }
        /// 接收客户端发来的信息 
        private void ServerRecMsg(object socketClientPara)
        {
            Socket socketServer = socketClientPara as Socket;
            while (true)
            {
                if (csName.pl[24, 1] == "否") { break; }
                //创建一个内存缓冲区 其大小为1024*1024字节  即1M
                byte[] arrServerRecMsg = new byte[1024 * 1024];
                try
                {
                    //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
                    int length = socketServer.Receive(arrServerRecMsg);
                    //将机器接受到的字节数组转换为人可以读懂的字符串
                    string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
                    if (strSRecMsg == csName.pl[16, 1])//验证成功执行
                    {
                        ServerSendMsgGraphThread = new Thread(ServerSendMsg) { IsBackground = true };
                        ServerSendMsgGraphThread.Start();
                        //创建一个通信线程 
                        ParameterizedThreadStart pts = new ParameterizedThreadStart(ServerRecMsgs);
                        Thread thr = new Thread(pts) { IsBackground = true };
                        //启动线程
                        thr.Start(socConnection);
                        socketWatch.Close();
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("远程客户端连接成功！");
                        }));
                        break;
                    }
                    else
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            WriteInfo("远程客户端已断开连接！");
                        }));
                        boolServerSendMsg = false;
                        try
                        {
                            socketWatch.Close();
                            socConnection.Close();
                        }
                        catch { }
                        if (csName.pl[24, 1] != "否") { ServerCon(); }
                        break;
                    }
                }
                catch
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("远程客户端已断开连接！");
                    }));
                    boolServerSendMsg = false;
                    if (csName.pl[24, 1] != "否") { ServerCon(); }
                    break;
                }
            }
        }
        /// 接收客户端发来的信息 
        private void ServerRecMsgs(object socketClientPara)
        {
            Socket socketServer = socketClientPara as Socket;
            while (true)
            {
                if (csName.pl[24, 1] == "否") { break; }
                //创建一个内存缓冲区 其大小为1024*1024字节  即1M
                byte[] arrServerRecMsg = new byte[1024 * 1024];
                try
                {
                    //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
                    int length = socketServer.Receive(arrServerRecMsg);
                    //将机器接受到的字节数组转换为人可以读懂的字符串
                    string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
                    //功能判断
                    switch (strSRecMsg)
                    {
                        case "1":
                            this.Invoke((EventHandler)(delegate
                            {
                                btnZdzx_Click(null, null);
                            }));
                            break;
                        case "2":
                            this.Invoke((EventHandler)(delegate
                            {
                                btnSjjl_Click(null, null);
                            }));
                            break;
                        case "3":
                            this.Invoke((EventHandler)(delegate
                            {
                                btnFwss_Click(null, null);
                            }));
                            break;
                        case "4":
                            this.Invoke((EventHandler)(delegate
                            {
                                btnFwml_Click(null, null);
                            }));
                            break;
                        case "5":
                            this.Invoke((EventHandler)(delegate
                            {
                                btnFyss_Click(null, null);
                            }));
                            break;
                        case "6":
                            this.Invoke((EventHandler)(delegate
                            {
                                btnFyml_Click(null, null);
                            }));
                            break;
                        default:
                            break;
                    }
                }
                catch
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("远程客户端已断开连接！");
                    }));
                    boolServerSendMsg = false;
                    if (csName.pl[24, 1] != "否") { ServerCon(); }
                    break;
                }
            }
        }
        // 发送信息到客户端的方法
        bool boolServerSendMsg = true;
        Thread ServerSendMsgGraphThread;
        private void ServerSendMsg()
        {
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    if (csName.pl[24, 1] == "否" || boolServerSendMsg == false) { break; }
                    //*卫星名,卫星圈次,开始时间,结束时间,
                    string strSend = "*" + csName.mxtTime + csName.mWxName + "," + csName.mQc + "," + csName.ksTime + "," + csName.mksTime + "," + csName.jsTime + "," + csName.fWjD + "," + csName.fYjD + "," + csName.fWYsjD + "," + csName.fYYsjD + "," + csName.fWjyjD + "," + csName.fYjyjD + "," + csName.AGC.ToString("0.000");
                    if (csName.boolAGC == true) { strSend += ",1"; } else { strSend += ",0"; }

                    //if (fwYk == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴告警
                    //if (fwSn == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴使能
                    //if (CWL == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴顺限
                    //if (fwszColor == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴顺转
                    //if (fwnzColor == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴逆转
                    //if (CCWL == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴逆限
                    //if (fwCc == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴超差
                    //if (fwCz == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴超载
                    //if (fwGr == true) { strSend += ",1"; } else { strSend += ",0"; }//X轴过热

                    //if (fyYk == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴告警
                    //if (DNL == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴上限
                    //if (fyszColor == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴顺转
                    //if (fynzColor == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴逆转
                    //if (UPL == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴下限
                    //if (fyqcColor == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴欠压
                    //if (fyczColor == true) { strSend += ",1"; } else { strSend += ",0"; }//Y轴超载
                    //if (jhYk == true) { strSend += ",1"; } else { strSend += ",0"; }//极化告警
                    //if (jhSn == true) { strSend += ",1"; } else { strSend += ",0"; } //极化使能
                    //if (PCWL == true) { strSend += ",1"; } else { strSend += ",0"; }//极化左旋
                    //if (PCCWL == true) { strSend += ",1"; } else { strSend += ",0"; }//极化右旋
                    socConnection.Send(Encoding.UTF8.GetBytes(strSend));
                    i = 0;
                    Thread.Sleep(20);
                }
            }
            catch
            {
                this.Invoke((EventHandler)(delegate
                {
                    WriteInfo("发送失败！");
                }));
            }
        }
        #endregion



        #region  方法
        public void WriteInfo(string infos)
        {
            try
            {
                lock (this)
                {
                    string time = DateTime.Now.ToString("HH:mm:ss");
                    if (Info.Items.Count > 500) { Info.Items.Remove(Info.Items[0]); }
                    Info.Items.Add(time + ">" + infos);
                    if (Info.Focused == false) { Info.SetSelected(Info.Items.Count - 1, true); }
                    csName.rzArray.Enqueue(time + ">" + infos);
                    if (csName.rzArray.Count > 50000)
                    {
                        if (!LogSave(rzLj.Text))//保存失败
                        {
                            Info.Items.Add(time + ">" + "数据存盘失败！");
                        }
                    }

                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "错误提示");
            }
        }
        private bool LogSave(string path)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    string lj = path + "\\log_" + DateTime.Now.ToString("yyyy年MM月dd日HH.mm.ss.fff") + ".txt";
                    FileStream fs1 = new FileStream(lj, FileMode.Create, FileAccess.Write);
                    File.SetAttributes(@lj, FileAttributes.Compressed);
                    StreamWriter sw = new StreamWriter(fs1);
                    for (int i = 0; i < csName.rzArray.Count; i++)
                    {
                        sw.WriteLine(csName.rzArray.ElementAt(i), Encoding.GetEncoding("utf-8"));
                    }
                    csName.rzArray = new Queue<string>(1);
                    sw.Close();
                    fs1.Close();
                    return true;
                }
                else return false;
            }
            catch { return false; }
        }
        #endregion

        private void BtnQxkj_Click(object sender, EventArgs e)
        {
            AxisControl.setEnable(2, true);
            Thread.Sleep(200);
            jh1.BackColor = Color.Lime;
            btnQxkj.BackColor = Color.Lime;
            btnQxdj.BackColor = Color.Silver;
            btnQxkj.Enabled = false;
            btnQxss.Enabled = true;
            btnQxml.Enabled = true;
            btnQxdj.Enabled = true;
            WriteInfo("极化已开机！");
        }

        private void BtnQxdj_Click(object sender, EventArgs e)
        {
            AxisControl.setMove(2, 0);
            AxisControl.setEnable(2, false);
            jh1.BackColor = Color.Silver;
            btnQxdj.BackColor = Color.Lime;
            btnQxkj.BackColor = Color.Silver;
            btnQxdj.Enabled = false;
            btnQxss.Enabled = false;
            btnQxml.Enabled = false;
            btnQxkj.Enabled = true;
            WriteInfo("极化待机中...");
        }

        private void BtnQxss_Click(object sender, EventArgs e)
        {
            if (AxisControl.getPosition(2) >= Convert.ToDouble(Main.csName.pl[42, 1]))
            {
                AxisControl.setSpeed(2, Convert.ToDouble(csName.pl[40, 1]));
                AxisControl.setMove(2, 2);
                WriteInfo("极化左旋命令已执行！");
            }
            else
            {
                WriteInfo("极化左限！");
            }

        }

        private void BtnQxml_Click(object sender, EventArgs e)
        {
            if (AxisControl.getPosition(2) <= Convert.ToDouble(Main.csName.pl[41, 1]))
            {
                AxisControl.setSpeed(2, Convert.ToDouble(csName.pl[40, 1]));
                AxisControl.setMove(2, 1);
                WriteInfo("极化右旋命令已执行！");
            }
            else
            {
                WriteInfo("极化右限！");
            }

        }

        private void 卫星计算器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "寻星精灵.exe");
        }

        private void 信标机设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "DvbsxCarEsti.exe");
        }
        #region 信标标校



        #endregion

        private void 两行根数辅助查看ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (zxBool == false)
            {
                f3.ShowDialog();
                if (f3.DialogResult == DialogResult.OK)
                {

                }
            }
            else
            {
                MessageBox.Show("请先取消\"自动执行\"!", "消息框");
            }
        }
        private void OnOff_Click(object sender, EventArgs e)//信标开关
        {
            try
            {
                if (btnOnOff.Text == "信标机 ON")
                {
                    agc.Open(csName.pl[37, 1].Split(',')[3], Convert.ToInt32(csName.pl[37, 1].Split(',')[7]));
                    btnOnOff.Text = "信标机 OFF"; groupBox1.Enabled = true; btnOnOff.BackColor = Color.Lime; xingbaioONOFF = 1;
                }
                else
                {
                    agc.Close();
                    btnOnOff.Text = "信标机 ON"; groupBox1.Enabled = false; btnOnOff.BackColor = Color.Silver; xingbaioONOFF = 0;
                }
            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message, "消息提示");
            }
        }
        private void Button3_Click(object sender, EventArgs e)//信标频率
        {
            try
            {
                agc.SetFreq(Convert.ToDouble(numericUpDown1.Value));
                WriteInfo("信标频率设置完成！");
            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message, "消息提示");
            }
        }
        double xunxingjingdu = 0.02;
        bool xunxing = false;//自动寻星
        bool saomiaogenzong = false;//扫描跟踪
        bool zhixing = false;//执行开启
        bool gengzong = false;//跟踪

        private void Button4_Click(object sender, EventArgs e)//自动寻星
        {
            try
            {
                if (btnFwkj.BackColor != Color.Lime) { WriteInfo("方位未使能！"); return; }
                if (btnFykj.BackColor != Color.Lime) { WriteInfo("俯仰未使能！"); return; }
                if (btnQxkj.BackColor != Color.Lime) { WriteInfo("极化未使能！"); return; }
                if (saomiaogenzong) { WriteInfo("请先取消天线扫描！"); return; }
                if (gengzong) { WriteInfo("请先取消天线跟踪！"); return; }
                if (zhixing) { WriteInfo("请先取消定位执行！"); return; }
                if (button4.BackColor == Color.Silver)
                {
                    //方位
                    double fwRxs = Convert.ToDouble(csName.pl[19, 1]);
                    double fwRxn = Convert.ToDouble(csName.pl[20, 1]);
                    if (csName.zxWzFw > fwRxs || csName.zxWzFw < fwRxn) { WriteInfo("方位轴超出软限位！"); return; }
                    //俯仰
                    fwRxs = Convert.ToDouble(csName.pl[21, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[22, 1]);
                    if (csName.zxWzFy > fwRxs || csName.zxWzFy < fwRxn) { WriteInfo("俯仰轴超出软限位！"); return; }
                    //极化
                    fwRxs = Convert.ToDouble(csName.pl[41, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[42, 1]);
                    if (csName.zxWzJh > fwRxs || csName.zxWzJh < fwRxn) { WriteInfo("极化轴超出软限位！"); return; }
                    xunxing = true;
                    if (Math.Abs(Convert.ToDouble(lblSjfw.Text) - Convert.ToDouble(lblYsfw.Text)) > xunxingjingdu)
                    {
                        AxisControl.setPosition(0, Convert.ToDouble(lblYsfw.Text), Convert.ToDouble(csName.pl[10, 1]));
                    }
                    if (Math.Abs(Convert.ToDouble(lblSjfy.Text) - Convert.ToDouble(lblYsfy.Text)) > xunxingjingdu)
                    {
                        AxisControl.setPosition(1, Convert.ToDouble(lblYsfy.Text), Convert.ToDouble(csName.pl[11, 1]));
                    }
                    if (Math.Abs(Convert.ToDouble(lblTxjdqx.Text) - Convert.ToDouble(lblTxmlqx.Text)) > xunxingjingdu)
                    {
                        AxisControl.setPosition(2, Convert.ToDouble(lblTxmlqx.Text), Convert.ToDouble(csName.pl[40, 1]));
                    }
                    #region 设置信标频率
                    double pp = 0;
                    if (csName.satelliteJd1[toolStripComboBox2.SelectedIndex]=="水平极化"|| csName.satelliteJd1[toolStripComboBox2.SelectedIndex] == "左旋极化")
                    {
                       pp = Convert.ToDouble(csName.satelliteJd2[toolStripComboBox2.SelectedIndex]);
                    }
                    else
                    {
                        pp = Convert.ToDouble(csName.satelliteJd3[toolStripComboBox2.SelectedIndex]);
                    }
                    if (pp > Convert.ToDouble(numericUpDown1.Maximum)) pp = Convert.ToDouble(numericUpDown1.Maximum);
                    if (pp < Convert.ToDouble(numericUpDown1.Minimum)) pp = Convert.ToDouble(numericUpDown1.Minimum);
                    numericUpDown1.Value = (decimal)pp;
                    agc.SetFreq(Convert.ToDouble(numericUpDown1.Value));
                    WriteInfo("设置信标频率完成！");
                    #endregion 

                    WriteInfo("自动寻星到位中...");
                    button4.BackColor = Color.Lime;
                    Task.Factory.StartNew(() =>
                    {
                        zhuangtai1 = new int[4] { 0, 0, 0, 0 };
                        int fw = 0; int fy = 0; int jh = 0;
                        while (true)
                        {
                            if (!xunxing)
                            {
                                zhuangtai1[0] = 1;
                                AxisControl.setMove(0, 0); AxisControl.setMove(1, 0); AxisControl.setMove(2, 0);
                                break;
                            }
                            if (fw == 0 && Math.Abs(Convert.ToDouble(lblSjfw.Text) - Convert.ToDouble(lblYsfw.Text)) <= xunxingjingdu)
                            {
                                AxisControl.setMove(0, 0); fw = 1;
                                this.Invoke((EventHandler)(delegate { WriteInfo("自动寻星方位已到位!"); })); zhuangtai1[1] = 1;
                            }
                            if (fy == 0 && Math.Abs(Convert.ToDouble(lblSjfy.Text) - Convert.ToDouble(lblYsfy.Text)) <= xunxingjingdu)
                            {
                                AxisControl.setMove(1, 0); fy = 1;
                                this.Invoke((EventHandler)(delegate { WriteInfo("自动寻星俯仰已到位!"); })); zhuangtai1[2] = 1;
                            }
                            if (jh == 0 && Math.Abs(Convert.ToDouble(lblTxjdqx.Text) - Convert.ToDouble(lblTxmlqx.Text)) <= xunxingjingdu)
                            {
                                AxisControl.setMove(2, 0); jh = 1;
                                this.Invoke((EventHandler)(delegate { WriteInfo("自动寻星极化已到位!"); })); zhuangtai1[3] = 1;
                            }
                            if (fw == 1 && fy == 1 && jh == 1)
                            {
                                zhuangtai1[0] = 0;
                                this.Invoke((EventHandler)(delegate { WriteInfo("自动寻星已完成!"); button4.BackColor = Color.Silver; })); xunxing = false; break;
                            }
                            Thread.Sleep(10);
                        }
                    });
                }
                else
                {
                    zhuangtai1[0] = 0;
                    xunxing = false;
                    WriteInfo("停止自动寻星！");
                    button4.BackColor = Color.Silver;
                }
            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message, "消息提示");
            }
        }
        double dingweijingdu = 0.03;//定位精度
        List<double> weizhi = new List<double>(0);
        List<double> agcData = new List<double>(0);
        double[] infoData = new double[8];
        double[] infoDataMax = new double[8];
        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnFwkj.BackColor != Color.Lime) { WriteInfo("方位未使能！"); return; }
                if (btnFykj.BackColor != Color.Lime) { WriteInfo("俯仰未使能！"); return; }
                if (btnQxkj.BackColor != Color.Lime) { WriteInfo("极化未使能！"); return; }
                if (zhixing) { WriteInfo("请先取消定位执行！"); return; }
                if (xunxing) { WriteInfo("请先取消自动寻星！"); return; }
                if (gengzong) { WriteInfo("请先取消卫星跟踪！"); return; }
                if (button1.BackColor == Color.Silver)
                {
                    //方位
                    double fwRxs = Convert.ToDouble(csName.pl[19, 1]);
                    double fwRxn = Convert.ToDouble(csName.pl[20, 1]);
                    if (csName.zxWzFw > fwRxs || csName.zxWzFw < fwRxn) { WriteInfo("方位轴超出软限位！"); return; }
                    //俯仰
                    fwRxs = Convert.ToDouble(csName.pl[21, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[22, 1]);
                    if (csName.zxWzFy > fwRxs || csName.zxWzFy < fwRxn) { WriteInfo("俯仰轴超出软限位！"); return; }
                    //极化
                    fwRxs = Convert.ToDouble(csName.pl[41, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[42, 1]);
                    if (csName.zxWzJh > fwRxs || csName.zxWzJh < fwRxn) { WriteInfo("极化轴超出软限位！"); return; }
                    saomiaogenzong = true;
                    double fwS = Convert.ToDouble(csName.pl[14, 1]) + AxisControl.getPosition(0);
                    double fwN = AxisControl.getPosition(0) - Convert.ToDouble(csName.pl[14, 1]);
                    double fwSp = Convert.ToDouble(csName.pl[12, 1]);
                    double fyS = Convert.ToDouble(csName.pl[15, 1]) + AxisControl.getPosition(1);
                    double fyN = +AxisControl.getPosition(1)- Convert.ToDouble(csName.pl[15, 1]);
                    double fySp = Convert.ToDouble(csName.pl[13, 1]);
                    double jhS = Convert.ToDouble(csName.pl[47, 1]) + AxisControl.getPosition(2);
                    double jhN = AxisControl.getPosition(2)- Convert.ToDouble(csName.pl[47, 1]);
                    double jhSp = Convert.ToDouble(csName.pl[48, 1]);
                    int cs = Convert.ToInt32(csName.pl[28, 1]);
                    WriteInfo("天线扫描中...");
                    button1.BackColor = Color.Lime;
                    Task.Factory.StartNew(() =>
                    {
                        int sat = 0;
                        while (true)
                        {
                            if (!saomiaogenzong)
                            {
                                button1.BackColor = Color.Silver; AxisControl.setMove(0, 0); AxisControl.setMove(1, 0); AxisControl.setMove(2, 0);
                                break;
                            }
                            if (sat == 0) //0方位到达扫描起始位置
                            {
                                zhuangtai2 = new int[4] { 1, 0, 0, 0 };
                                this.Invoke((EventHandler)(delegate
                                {
                                    fwS = Convert.ToDouble(csName.pl[14, 1]) + AxisControl.getPosition(0);
                                    fwN = AxisControl.getPosition(0) - Convert.ToDouble(csName.pl[14, 1]);
                                    fwSp = Convert.ToDouble(csName.pl[12, 1]);
                                    fyS = Convert.ToDouble(csName.pl[15, 1]) + AxisControl.getPosition(1);
                                    fyN = +AxisControl.getPosition(1) - Convert.ToDouble(csName.pl[15, 1]);
                                    fySp = Convert.ToDouble(csName.pl[13, 1]);
                                    jhS = Convert.ToDouble(csName.pl[47, 1]) + AxisControl.getPosition(2);
                                    jhN = AxisControl.getPosition(2) - Convert.ToDouble(csName.pl[47, 1]);
                                    jhSp = Convert.ToDouble(csName.pl[48, 1]);
                                }));
                                AxisControl.setPosition(0, fwN, fwSp); sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("方位向起始位置运动中..."); }));
                            }
                            if (sat == 1) //2方位识别到位
                            {
                                if (Math.Abs(fwN - AxisControl.getPosition(0)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(0, 0); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("方位到达起始位置开始扫描..."); }));
                                }
                            }
                            if (sat == 2) //2方位运动到扫描扫描终止
                            {
                                AxisControl.setPosition(0, fwS, fwSp); sat++;
                                weizhi = new List<double>(0);
                                agcData = new List<double>(0);
                            }
                            if (sat == 3) //3记录最大AGC位置
                            {
                                weizhi.Add(AxisControl.getPosition(0));
                                agcData.Add(agc.GetAGC());
                                if (Math.Abs(fwS - AxisControl.getPosition(0)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(0, 0); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("方位扫描完成..."); })); zhuangtai2[1] = 1;
                                    infoData[0] = agcData.Max();
                                    infoData[1] = weizhi[agcData.IndexOf(infoData[0])];
                                    this.Invoke((EventHandler)(delegate { WriteInfo("最大AGC为" + infoData[0].ToString("0.00") + ",对应方位角为" + infoData[1].ToString("0.00")); }));
                                }
                            }
                            if (sat == 4) //4方位回最大，
                            {
                                AxisControl.setPosition(0, infoData[1], fwSp);
                                sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("方位向最大位置运动中..."); }));
                            }
                            if (sat == 5) //5俯仰识别到位
                            {
                                if (Math.Abs(infoData[1] - AxisControl.getPosition(0)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(0, 0);
                                    sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("方位到达最大位置"); })); 
                                }
                            }
                            if (sat == 6) //4方位回最大，俯仰到达扫描起始位置
                            {
                                AxisControl.setPosition(1, fyN, fySp);
                                sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("俯仰向起始位置运动中..."); }));
                            }
                            if (sat == 7) //5俯仰识别到位
                            {
                                if (Math.Abs(fyN - AxisControl.getPosition(1)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(1, 0);
                                    sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("俯仰到达起始位置开始扫描..."); }));
                                }
                            }
                            if (sat == 8) //6俯仰运动到扫描扫描终止
                            {
                                AxisControl.setPosition(1, fyS, fySp); sat++;
                                weizhi = new List<double>(0);
                                agcData = new List<double>(0);
                            }
                            if (sat == 9) //7记录最大AGC位置
                            {
                                weizhi.Add(AxisControl.getPosition(1));
                                agcData.Add(agc.GetAGC());
                                if (Math.Abs(fyS - AxisControl.getPosition(1)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(1, 0); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("俯仰扫描完成..."); })); zhuangtai2[2] = 1;
                                    infoData[2] = agcData.Max();
                                    infoData[3] = weizhi[agcData.IndexOf(infoData[2])];
                                    this.Invoke((EventHandler)(delegate { WriteInfo("最大AGC为" + infoData[2].ToString("0.00") + ",对应俯仰角为" + infoData[3].ToString("0.00")); }));
                                }
                            }
                            if (sat == 10) //8俯仰回最大，极化到达扫描起始位置
                            {
                                AxisControl.setPosition(1, infoData[3], fySp);
                                sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("俯仰向最大位置运动中..."); }));
                            }
                            if (sat == 11) //9极化识别到位
                            {
                                if (Math.Abs(infoData[3] - AxisControl.getPosition(1)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(1, 0);
                                    sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("俯仰到达最大位置"); }));
                                }
                            }
                            if (sat == 12) //13极化到位
                            {
                                if (Math.Abs(infoData[3] - AxisControl.getPosition(1)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(1, 0);
                                    if (cs <= 1)
                                    {
                                        this.Invoke((EventHandler)(delegate { WriteInfo("扫描跟踪结束！"); })); zhuangtai2[0] = 0;
                                        saomiaogenzong = false;
                                    }
                                    else
                                    {
                                        this.Invoke((EventHandler)(delegate { WriteInfo("开始下一次扫描跟踪..."); })); zhuangtai2 = new int[4] { 1, 0, 0, 0 };
                                        cs--; sat = 0;
                                    }

                                }
                            }
                            //if (sat == 12) //8俯仰回最大，极化到达扫描起始位置
                            //{
                            //    axisControl.setPosition(2, jhN, jhSp);
                            //    sat++;
                            //    this.Invoke((EventHandler)(delegate { writeInfo("极化向起始位置运动中..."); }));
                            //}
                            //if (sat == 13) //9极化识别到位
                            //{
                            //    if (Math.Abs(jhN - axisControl.getPosition(2)) <= dingweijingdu)
                            //    {
                            //       axisControl.setMove(2, 0);
                            //        sat++;
                            //        this.Invoke((EventHandler)(delegate { writeInfo("极化到达起始位置开始扫描..."); }));
                            //    }
                            //}
                            //if (sat == 14) //10极化运动到扫描扫描终止
                            //{
                            //    axisControl.setPosition(2, jhS, jhSp); sat++;
                            //    weizhi = new List<double>(0);
                            //    agcData = new List<double>(0);
                            //}
                            //if (sat == 15) //11记录最大AGC位置
                            //{
                            //    weizhi.Add(axisControl.getPosition(2));
                            //    agcData.Add(agc.getAGC());
                            //    if (Math.Abs(jhS - axisControl.getPosition(2)) <= dingweijingdu)
                            //    {
                            //        axisControl.setMove(2, 0); sat++;
                            //        this.Invoke((EventHandler)(delegate { writeInfo("极化扫描完成..."); }));zhuangtai2[3] = 1;
                            //        infoData[4] = agcData.Max();
                            //        infoData[5] = weizhi[agcData.IndexOf(infoData[4])];
                            //        this.Invoke((EventHandler)(delegate {  writeInfo("最大AGC为" + infoData[4].ToString("0.00") + ",对应极化角为" + infoData[5].ToString("0.00")); }));
                            //    }
                            //}
                            //if (sat == 16) //12极化到位中
                            //{
                            //    axisControl.setPosition(2, infoData[5], jhSp);
                            //    sat++;
                            //    this.Invoke((EventHandler)(delegate { writeInfo("极化到位中..."); }));
                            //}
                            //if (sat == 17) //13极化到位
                            //{
                            //    if (Math.Abs(infoData[5] - axisControl.getPosition(2)) <= dingweijingdu)
                            //    {
                            //        axisControl.setMove(2, 0);
                            //        if (cs <= 1)
                            //        {
                            //            this.Invoke((EventHandler)(delegate { writeInfo("扫描跟踪结束！"); }));
                            //            saomiaogenzong = false;
                            //        }
                            //        else
                            //        {
                            //            this.Invoke((EventHandler)(delegate { writeInfo("开始下一次扫描跟踪..."); }));
                            //            cs--; sat = 0;
                            //        }

                            //    }
                            //}
                            Thread.Sleep(20);
                        }
                    });
                }
                else
                {
                    zhuangtai2[0] = 0;
                    saomiaogenzong = false;
                    WriteInfo("天线扫描取消！");
                    button1.BackColor = Color.Silver;
                }
            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message, "消息提示");
            }
        }
        //天线定位
        //执行
        int boolJh = 0;
        private void BtnZx_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnFwkj.BackColor != Color.Lime) { WriteInfo("方位未开机！"); return; }
                if (btnFykj.BackColor != Color.Lime) { WriteInfo("俯仰未开机！"); return; }
                if (btnQxkj.BackColor != Color.Lime) { WriteInfo("极化未开机！"); return; }
                if (btnZx.BackColor == Color.Lime) return;
                if (saomiaogenzong) { WriteInfo("请先取消扫描！"); return; }
                if (gengzong) { WriteInfo("请先取消天线跟踪！"); return; }
                if (xunxing) { WriteInfo("请先取消自动寻星！"); return; }
                if (zxBool != true)
                {
                    if (!double.TryParse(txtFwml.Text.Trim(), out csName.zxWzFw) || !double.TryParse(txtFyml.Text.Trim(), out csName.zxWzFy) || !double.TryParse(txtJhml.Text.Trim(), out csName.zxWzJh))
                    {
                        WriteInfo("天线命令角度格式不正确！");return;
                    }
                    //方位
                    double fwRxs = Convert.ToDouble(csName.pl[19, 1]);
                    double fwRxn = Convert.ToDouble(csName.pl[20, 1]);
                    if (csName.zxWzFw > fwRxs || csName.zxWzFw < fwRxn) { WriteInfo("方位轴超出软限位！"); return; }
                    //俯仰
                    fwRxs = Convert.ToDouble(csName.pl[21, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[22, 1]);
                    if (csName.zxWzFy > fwRxs || csName.zxWzFy < fwRxn) { WriteInfo("俯仰轴超出软限位！"); return; }
                    //极化
                    fwRxs = Convert.ToDouble(csName.pl[41, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[42, 1]);
                    if (csName.zxWzJh > fwRxs || csName.zxWzJh < fwRxn) { WriteInfo("极化轴超出软限位！"); return; }
                    //
                    AxisControl.setPosition(0, csName.zxWzFw, Convert.ToDouble(csName.pl[10, 1]));
                    AxisControl.setPosition(1, csName.zxWzFy, Convert.ToDouble(csName.pl[11, 1]));
                    AxisControl.setPosition(2, csName.zxWzJh, Convert.ToDouble(csName.pl[40, 1]));
                    //
                    boolFw = boolFy = boolJh = 0;
                    zhixing = true;
                    WriteInfo("天线命令执行中...");
                    btnZx.BackColor = Color.Lime;
                    btnZx.Enabled = false;
                  //
                    Task.Factory.StartNew(() =>
                    {
                        while (true)
                        {
                            if (!zhixing)
                            {
                                AxisControl.setMove(0, 0);//X轴0速
                                AxisControl.setMove(1, 0);//Y轴0速
                                AxisControl.setMove(2, 0);//Y轴0速
                                boolFw = boolFy = boolJh = 0;
                                break;
                            }
                            if (boolFw == 0&& csName.zxWzFw <= (csName.fWjD + Convert.ToDouble(csName.dwJd[2])) && csName.zxWzFw >= (csName.fWjD - Convert.ToDouble(csName.dwJd[3])))
                            {
                                AxisControl.setMove(0, 0);AxisControl.setMove(0, 0);//X轴0速
                                this.Invoke((EventHandler)(delegate { WriteInfo("方位轴位置就位！"); boolFw = 1; }));
                            }
                            if (boolFy == 0 && csName.zxWzFy <= (csName.fYjD + Convert.ToDouble(csName.dwJd[4])) && csName.zxWzFy >= (csName.fYjD - Convert.ToDouble(csName.dwJd[5])))
                            {
                                AxisControl.setMove(1, 0); AxisControl.setMove(1, 0);
                                this.Invoke((EventHandler)(delegate{WriteInfo("俯仰轴位置就位！"); boolFy = 1; }));
                            }
                            if (boolJh == 0 && csName.zxWzJh <= (csName.PjD + Convert.ToDouble(csName.dwJd[6])) && csName.zxWzJh >= (csName.PjD - Convert.ToDouble(csName.dwJd[7])))
                            {
                                AxisControl.setMove(2, 0); AxisControl.setMove(2, 0);
                                this.Invoke((EventHandler)(delegate { WriteInfo("极化轴位置就位！"); boolJh = 1; }));
                            }
                            if (boolFw == 1&& boolFy == 1 && boolJh == 1)//所有轴到位
                            {
                                zhixing = false;
                                this.Invoke((EventHandler)(delegate { WriteInfo("天线命令执行完成！"); btnZx.BackColor = Color.Silver; btnZx.Enabled = true; }));
                                break;
                            }
                            Thread.Sleep(10);
                        }
                    });
                }
                else
                {
                    WriteInfo("请先关闭'自动执行'后再执行'天线命令'！");
                }
            }
            catch (Exception err) { WriteInfo(err.Message); }
        }
        //X轴命令线程
        Thread zxFwGraphThread; int boolFw = 0;
        public void ZxFwMl()
        {
            try
            {
                double fwRxs = Convert.ToDouble(csName.pl[19, 1]);
                double fwRxn = Convert.ToDouble(csName.pl[20, 1]);
                if (csName.zxWzFw > fwRxs || csName.zxWzFw < fwRxn)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("方位轴超出软限位！"); btnZx.BackColor = Color.Silver; btnZx.Enabled = true; return;
                    }));
                }
                AxisControl.setPosition(0, csName.zxWzFw, Convert.ToDouble(csName.pl[10, 1]));
                while (true)
                {
                    if (boolFw == 0) break;
                    //限位

                    //if (AxisControl.getLimit(0) == 1 || AxisControl.getLimit(0) == 2 || !zhixing) { AxisControl.setMove(0, 0); break; }
                    if (!zhixing) { AxisControl.setMove(0, 0); break; }
                    if (zxBool == true)
                    {
                        AxisControl.setMove(0, 0);//X轴0速
                        this.Invoke((EventHandler)(delegate
                        {
                            btnZx.BackColor = Color.Silver; btnZx.Enabled = true;
                        }));
                        break;
                    }
                    if (csName.zxWzFw <= (csName.fWjD + Convert.ToDouble(csName.dwJd[2])) && csName.zxWzFw >= (csName.fWjD - Convert.ToDouble(csName.dwJd[3])))
                    {
                        AxisControl.setMove(0, 0);//X轴0速
                        this.Invoke((EventHandler)(delegate
                        {
                           
                            WriteInfo("方位轴位置就位！");
                            AxisControl.setMove(0, 0);//X轴0速
                            boolFw = 0;
                            if (boolFy == 0 && boolFw == 0)
                            {
                                zhixing = false; btnZx.BackColor = Color.Silver; btnZx.Enabled = true;
                            }
                        }));
                        break;
                    }
                    Thread.Sleep(20);
                }
            }
            catch
            {
            }
        }
        //Y轴命令线程
        Thread zxFyGraphThread; int boolFy = 0;
        public void ZxFyMl()
        {
            try
            {
                double fwRxs = Convert.ToDouble(csName.pl[21, 1]);
                double fwRxn = Convert.ToDouble(csName.pl[22, 1]);
                if (csName.zxWzFy > fwRxs || csName.zxWzFy < fwRxn)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        WriteInfo("俯仰轴超出软限位！"); btnZx.BackColor = Color.Silver; btnZx.Enabled = true; return;
                    }));
                }
                AxisControl.setPosition(1, csName.zxWzFy, Convert.ToDouble(csName.pl[11, 1]));
                while (true)
                {
                    if (boolFy == 0) break;
                    //限位
                    //if (AxisControl.getLimit(1) == 1 || AxisControl.getLimit(1) == 2|| !zhixing) { AxisControl.setMove(1, 0); break; }
                    if (!zhixing) { AxisControl.setMove(1, 0); break; }
                    if (zxBool == true)
                    {
                        AxisControl.setMove(1, 0);
                        this.Invoke((EventHandler)(delegate
                        {
                            btnZx.BackColor = Color.Silver; btnZx.Enabled = true;
                        }));
                        break;
                    }
                    if (csName.zxWzFy <= (csName.fYjD + Convert.ToDouble(csName.dwJd[4])) && csName.zxWzFy >= (csName.fYjD - Convert.ToDouble(csName.dwJd[5])))
                    {
                        AxisControl.setMove(1, 0);
                        this.Invoke((EventHandler)(delegate
                        {
                            
                            WriteInfo("俯仰轴位置就位！");
                            AxisControl.setMove(1, 0);
                            boolFy = 0;
                            if(boolFy == 0&& boolFw == 0)
                            {
                                zhixing = false; btnZx.BackColor = Color.Silver; btnZx.Enabled = true;
                            }
                           
                        }));
                        break;
                    }
                    Thread.Sleep(20);
                }
            }
            catch
            {
            }
        }
        //取消
        private void BtnQx_Click(object sender, EventArgs e)
        {
            try
            {
                zhixing = false;
                AxisControl.setMove(0, 0);//X轴0速
                AxisControl.setMove(1, 0);//Y轴0速
                AxisControl.setMove(2, 0);//Y轴0速
                WriteInfo("天线命令已取消！");
                btnZx.BackColor = Color.Silver;
                btnZx.Enabled = true;
                
            }
            catch
            {
            }
        }

        private void ToolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (zxBool == false)
            {
                f4.ShowDialog();
                if (f4.DialogResult == DialogResult.OK)
                {

                }
            }
            else
            {
                MessageBox.Show("请先取消\"自动执行\"!", "消息框");
            }
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "寻星精灵.exe");
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            Process.Start(AppDomain.CurrentDomain.BaseDirectory + "DvbsxCarEsti.exe");
        }

        UdpClient UdpClientConnect;         //接UdpClent接口
        IPEndPoint UdpRecive;     //Udp接收节点
        IPEndPoint UdpSend;       //Udp发送节点
        Thread ThreadReciveScm;
        Thread ThreadSendScm;
        private void BtnZhxxLeft_Click(object sender, EventArgs e)
        {
            if (btnZhxxLeft.BackColor == Color.Silver)
            {

                boolServerSendMsg = true;
                UdpClientConnect = new UdpClient(Convert.ToInt32(csName.pl[5, 1]) +1000);
                UdpRecive = new IPEndPoint(IPAddress.Parse(csName.pl[4, 1]), 0);
                UdpSend = new IPEndPoint(IPAddress.Parse(csName.pl[4, 1]), Convert.ToInt32(csName.pl[5, 1]));


                ThreadReciveScm = new Thread(ReciveScm) { IsBackground = true };
                ThreadReciveScm.Start();
                ThreadSendScm = new Thread(SendScm) { IsBackground = true };
                ThreadSendScm.Start();
                btnZhxxLeft.BackColor = Color.Lime;
                ipAddress.Enabled = false; port.Enabled = false; inforVer.Enabled = false;
                WriteInfo("已开启远程监控!");
                menuStrip1.Enabled = tabControl1.Enabled= tbBX.Enabled= panel4.Enabled= btnTdmx.Enabled=false;
            }
            else
            {
                boolServerSendMsg = false;
                Thread.Sleep(1000);
                ipAddress.Enabled = true; port.Enabled = true; inforVer.Enabled = true;
                WriteInfo("已关闭远程监控!");
                btnZhxxLeft.BackColor = Color.Silver;
                menuStrip1.Enabled = tabControl1.Enabled = tbBX.Enabled = panel4.Enabled = btnTdmx.Enabled = true;
            }
        }
        private void ReciveScm()
        {
            short zhaungtai = 0;
            byte[] DataBuffer = new byte[1];
            while (true)
            {
                try
                {
                    if (!boolServerSendMsg) { UdpClientConnect.Close(); UdpClientConnect = null; break; }
                    if (UdpClientConnect.Available > 0)
                    {
                        DataBuffer = UdpClientConnect.Receive(ref UdpRecive);
                        if ( DataBuffer[0] == 0xFF && DataBuffer[1] == 0x07 && DataBuffer[DataBuffer.Length - 2] == 0x7D && DataBuffer[DataBuffer.Length - 1] == 0xAA)
                        {
                            switch (DataBuffer[2])
                            {
                                case 0x01:
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        if (DataBuffer[3] == 1)
                                        {
                                            btnFwkj_Click(null, null);
                                        }
                                        else
                                        {
                                            btnFwdj_Click(null, null);
                                        }
                                    }));
                                    break;
                                case 0x02:
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        if (DataBuffer[3] == 1)
                                        {
                                            btnFykj_Click(null, null);
                                        }
                                        else
                                        {
                                            btnFydj_Click(null, null);
                                        }
                                    }));
                                    break;
                                case 0x03:
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        if (DataBuffer[3] == 1)
                                        {
                                            BtnQxkj_Click(null, null);
                                        }
                                        else
                                        {
                                            BtnQxdj_Click(null, null);
                                        }
                                    }));
                                    break;
                                case 0x04://设置卫星ID
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        toolStripComboBox2.Text = toolStripComboBox2.Items[BitConverter.ToUInt16(DataBuffer, 3)].ToString();
                                        toolStripComboBox2_DropDownClosed(null, null);
                                    }));
                                    break;
                                case 0x05:// 自动寻星
                                    this.Invoke((EventHandler)(delegate
                                    {

                                        if (DataBuffer[3] == 1)
                                        {
                                            button4.BackColor = Color.Silver;
                                        }
                                        else
                                        {
                                            button4.BackColor = Color.Lime;
                                        }
                                        Button4_Click(null, null);
                                    }));
                                    break;
                                case 0x06:// 天线扫描
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        if (btnOnOff.Text == "信标机 ON")
                                        {
                                            if (DataBuffer[3] == 1)
                                            {
                                                button1.BackColor = Color.Silver;
                                            }
                                            else
                                            {
                                                button1.BackColor = Color.Lime;
                                            }
                                            Button1_Click(null, null);
                                        }
                                    }));
                                    break;
                                case 0x07:// 天线跟踪
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        if (btnOnOff.Text == "信标机 ON")
                                        {
                                            if (DataBuffer[3] == 1)
                                            {
                                                button6.BackColor = Color.Silver;
                                            }
                                            else
                                            {
                                                button6.BackColor = Color.Lime;
                                            }
                                            button6_Click(null, null);
                                        }
                                    }));
                                    break;
                                case 0x08://上报时间间隔
                                    sleepTime = BitConverter.ToUInt16(DataBuffer, 3);
                                    break;
                                case 0x09:// 信标机开启
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        if (DataBuffer[3] == 1)
                                        {
                                            btnOnOff.Text = "信标机 ON";
                                        }
                                        else
                                        {
                                            btnOnOff.Text = "信标机 OFF";
                                        }
                                        OnOff_Click(null, null);
                                    }));
                                    break;
                                case 0x0A:// 修改信标频率
                                    this.Invoke((EventHandler)(delegate
                                    {
                                        numericUpDown1.Value = (decimal)BitConverter.ToSingle(DataBuffer, 3);       //28调试错误
                                        Button3_Click(null, null);//信标频率
                                    }));
                                    break;
                                case 0x10:// 轴停止
                                    zhaungtai = DataBuffer[3];
                                    AxisControl.setMove(zhaungtai, 0);
                                    break;
                                case 0x11:// 轴顺转
                                    zhaungtai = DataBuffer[3];
                                    switch (zhaungtai)
                                    {
                                        case 0: AxisControl.setSpeed(zhaungtai, Convert.ToDouble(csName.pl[10, 1])); break;
                                        case 1: AxisControl.setSpeed(zhaungtai, Convert.ToDouble(csName.pl[11, 1])); break;
                                        case 2: AxisControl.setSpeed(zhaungtai, Convert.ToDouble(csName.pl[40, 1])); break;
                                    }
                                    AxisControl.setMove(zhaungtai, 1);
                                    break;
                                case 0x12:// 轴逆转
                                    zhaungtai = DataBuffer[3];
                                    switch (zhaungtai)
                                    {
                                        case 0: AxisControl.setSpeed(zhaungtai, Convert.ToDouble(csName.pl[10, 1])); break;
                                        case 1: AxisControl.setSpeed(zhaungtai, Convert.ToDouble(csName.pl[11, 1])); break;
                                        case 2: AxisControl.setSpeed(zhaungtai, Convert.ToDouble(csName.pl[40, 1])); break;
                                    }
                                    AxisControl.setMove(zhaungtai, 2);
                                    break;
                            }
                            gongnengnum = DataBuffer[2];//状态码
                        }
                    }
                    Thread.Sleep(10);
                }
                catch { }
            }
        }
        Byte gongnengnum = 0;
        UInt16 sleepTime = 100;
        private void SendScm()
        {
            byte[] sendData = new byte[62];
            while (true)
            {
                try
                {
                    byte state = 0;
                    UInt16 errNum = 0;
                    if (!boolServerSendMsg) break;
                    sendData[0] = 0x55;//前导码
                    sendData[1] = 0x7B;//帧头
                    sendData[2] = Convert.ToByte(sendData.Length - 6);//数据长
                    //方位
                    BitConverter.GetBytes(csName.fWjD).CopyTo(sendData, 3);//方位角度
                    BitConverter.GetBytes(Convert.ToUInt16(AxisControl.getSpeed(0))).CopyTo(sendData, 11);//方位速度
                    if (AxisControl.getEnable(0)) state += 0x01;//使能
                    switch(AxisControl.getState(0))
                    {
                        case 0: state += 0x02; break;
                        case 1: state += 0x04; break;
                        case 2: state += 0x08; break;
                    }
                    switch (AxisControl.getLimit(0))
                    {
                        case 1: state += 0x10; break;
                        case 2: state += 0x20; break;
                    }
                    if (AxisControl.getErrStr(0)!= "NONE") state += 0x40;//
                    sendData[13] = state; state = 0;//状态
                    switch(AxisControl.getErrStr(0))
                    {
                        case "NONE": errNum = 0x0000; break;
                        case "异常": errNum = 0x0015; break;
                        case "过压": errNum = 0x0011; break;
                        case "过载": errNum = 0x0012; break;
                        case "欠压": errNum = 0x0013; break;
                        case "超速": errNum = 0x0014; break;
                    }
                    BitConverter.GetBytes(errNum).CopyTo(sendData, 14); errNum = 0;//错误码
                    //俯仰
                    BitConverter.GetBytes(csName.fYjD).CopyTo(sendData, 16);//角度
                    BitConverter.GetBytes(Convert.ToUInt16(AxisControl.getSpeed(1))).CopyTo(sendData, 24);//方位速度
                    if (AxisControl.getEnable(1)) state += 0x01;//使能
                    switch (AxisControl.getState(1))
                    {
                        case 0: state += 0x02; break;
                        case 1: state += 0x04; break;
                        case 2: state += 0x08; break;
                    }
                    switch (AxisControl.getLimit(1))
                    {
                        case 1: state += 0x10; break;
                        case 2: state += 0x20; break;
                    }
                    if (AxisControl.getErrStr(1) != "NONE") state += 0x40;//
                    sendData[26] = state; state = 0;//状态
                    switch (AxisControl.getErrStr(1))
                    {
                        case "NONE": errNum = 0x0000; break;
                        case "异常": errNum = 0x0015; break;
                        case "过压": errNum = 0x0011; break;
                        case "过载": errNum = 0x0012; break;
                        case "欠压": errNum = 0x0013; break;
                        case "超速": errNum = 0x0014; break;
                    }
                    BitConverter.GetBytes(errNum).CopyTo(sendData, 27); errNum = 0;//错误码
                    //极化
                    BitConverter.GetBytes(csName.PjD).CopyTo(sendData, 29);//角度
                    BitConverter.GetBytes(Convert.ToUInt16(AxisControl.getSpeed(2))).CopyTo(sendData, 37);//方位速度
                    if (AxisControl.getEnable(2)) state += 0x01;//使能
                    switch (AxisControl.getState(2))
                    {
                        case 0: state += 0x02; break;
                        case 1: state += 0x04; break;
                        case 2: state += 0x08; break;
                    }
                    switch (AxisControl.getLimit(2))
                    {
                        case 1: state += 0x10; break;
                        case 2: state += 0x20; break;
                    }
                    if (AxisControl.getErrStr(2) != "NONE") state += 0x40;//
                    sendData[39] = state; state = 0;//状态
                    switch (AxisControl.getErrStr(2))
                    {
                        case "NONE": errNum = 0x0000; break;
                        case "异常": errNum = 0x0015; break;
                        case "过压": errNum = 0x0011; break;
                        case "过载": errNum = 0x0012; break;
                        case "欠压": errNum = 0x0013; break;
                        case "超速": errNum = 0x0014; break;
                    }
                    BitConverter.GetBytes(errNum).CopyTo(sendData, 40); errNum = 0;//错误码
                    //
                    BitConverter.GetBytes(Convert.ToUInt16(agc.GetAGC()*100)).CopyTo(sendData, 42);//电压
                    BitConverter.GetBytes(Convert.ToSingle(agc.GetFreq())).CopyTo(sendData, 45);//频率
                    BitConverter.GetBytes(Convert.ToUInt16(xingID)).CopyTo(sendData, 49);//卫星ID
                    //
                    if (zhuangtai1[0] == 1) state += 0x01;
                    if (zhuangtai1[1] == 1) state += 0x02;
                    if (zhuangtai1[2] == 1) state += 0x04;
                    if (zhuangtai1[3] == 1) state += 0x08;
                    sendData[51] = state; state = 0;
                    if (zhuangtai2[0] == 1) state += 0x01;
                    if (zhuangtai2[1] == 1) state += 0x02;
                    if (zhuangtai2[2] == 1) state += 0x04;
                    if (zhuangtai2[3] == 1) state += 0x08;
                    sendData[52] = state; state = 0;
                    if (zhuangtai3[0] == 1) state += 0x01;
                    if (zhuangtai3[1] == 1) state += 0x02;
                    if (zhuangtai3[2] == 1) state += 0x04;
                    if (zhuangtai3[3] == 1) state += 0x08;
                    sendData[53] = state; state = 0;
                    //
                    sendData[54] = gongnengnum;//上次接受功能码
                    BitConverter.GetBytes(Convert.ToUInt16(sleepTime)).CopyTo(sendData, 55);//上报时间间隔
                    sendData[57]= xingbaioONOFF;//信标状态
                    SerialPortBase.CalculateCRC(sendData, sendData.Length-4).CopyTo(sendData, 58);//校验码
                    sendData[sendData.Length - 2] = 0x7D;
                    sendData[sendData.Length - 1] = 0xAA;
                    UdpClientConnect.Send(sendData, sendData.Length, UdpSend);
                    Thread.Sleep(sleepTime);
                }
                catch{ }
            }
        }
        byte xingbaioONOFF = 0;
        int[] zhuangtai1 = new int[4] { 0, 0, 0 ,0};
        int[] zhuangtai2 = new int[4] { 0, 0, 0, 0 };
        int[] zhuangtai3 = new int[4] { 0, 0, 0, 0 };
        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnFwkj.BackColor != Color.Lime) { WriteInfo("方位未使能！"); return; }
                if (btnFykj.BackColor != Color.Lime) { WriteInfo("俯仰未使能！"); return; }
                if (btnQxkj.BackColor != Color.Lime) { WriteInfo("极化未使能！"); return; }
                if (zhixing) { WriteInfo("请先取消定位执行！"); return; }
                if (saomiaogenzong) { WriteInfo("请先取消天线扫描！"); return; }
                if (xunxing) { WriteInfo("请先取消自动寻星！"); return; }
                if (button6.BackColor == Color.Silver)
                {
                    //方位
                    double fwRxs = Convert.ToDouble(csName.pl[19, 1]);
                    double fwRxn = Convert.ToDouble(csName.pl[20, 1]);
                    if (csName.zxWzFw > fwRxs || csName.zxWzFw < fwRxn) { WriteInfo("方位轴超出软限位！"); return; }
                    //俯仰
                    fwRxs = Convert.ToDouble(csName.pl[21, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[22, 1]);
                    if (csName.zxWzFy > fwRxs || csName.zxWzFy < fwRxn) { WriteInfo("俯仰轴超出软限位！"); return; }
                    //极化
                    fwRxs = Convert.ToDouble(csName.pl[41, 1]);
                    fwRxn = Convert.ToDouble(csName.pl[42, 1]);
                    if (csName.zxWzJh > fwRxs || csName.zxWzJh < fwRxn) { WriteInfo("极化轴超出软限位！"); return; }
                    gengzong = true;
                    double fwS = Convert.ToDouble(csName.pl[14, 1]) + AxisControl.getPosition(0);
                    double fwN = AxisControl.getPosition(0) - Convert.ToDouble(csName.pl[14, 1]);
                    double fwSp = Convert.ToDouble(csName.pl[12, 1]);
                    double fyS = Convert.ToDouble(csName.pl[15, 1]) + AxisControl.getPosition(1);
                    double fyN = +AxisControl.getPosition(1) - Convert.ToDouble(csName.pl[15, 1]);
                    double fySp = Convert.ToDouble(csName.pl[13, 1]);
                    double jhS = Convert.ToDouble(csName.pl[47, 1]) + AxisControl.getPosition(2);
                    double jhN = AxisControl.getPosition(2) - Convert.ToDouble(csName.pl[47, 1]);
                    double jhSp = Convert.ToDouble(csName.pl[48, 1]);
                    int cs = Convert.ToInt32(csName.pl[28, 1]);
                    WriteInfo("天线跟踪中...");
                    button6.BackColor = Color.Lime;
                    Task.Factory.StartNew(() =>
                    {
                        int sat = 0;
                        while (true)
                        {
                            if (!gengzong)
                            {
                                button6.BackColor = Color.Silver; AxisControl.setMove(0, 0); AxisControl.setMove(1, 0); AxisControl.setMove(2, 0);
                                break;
                            }
                            if (sat == 0) //0方位到达扫描起始位置
                            {
                                zhuangtai3 = new int[4] { 1, 0, 0, 0 };
                                AxisControl.setSpeed(0, fwSp);
                                AxisControl.setMove(0, 2); sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("方位跟踪中..."); }));
                                weizhi = new List<double>(0);
                                agcData = new List<double>(0);
                            }
                            if (sat == 1) //2方位识别到位
                            {
                                weizhi.Add(AxisControl.getPosition(0));
                                agcData.Add(agc.GetAGC());
                                if (agcData.Count>2&& agcData[agcData.Count-1]< agcData[agcData.Count-2])
                                {
                                    AxisControl.setSpeed(0, fwSp);
                                    AxisControl.setMove(0, 1); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("方位换向跟踪中..."); }));
                                    infoData[0] = agcData[agcData.Count-1];
                                    infoData[1] = weizhi[weizhi.Count-1];
                                    this.Invoke((EventHandler)(delegate { WriteInfo("最小AGC为" + infoData[0].ToString("0.00") + ",对应方位角为" + infoData[1].ToString("0.00")); }));
                                    weizhi = new List<double>(0);
                                    agcData = new List<double>(0);
                                }
                            }
                            if (sat == 2) //2方位运动到扫描扫描终止
                            {
                                weizhi.Add(AxisControl.getPosition(0));
                                agcData.Add(agc.GetAGC());
                                if (agcData.Count > 2 && agcData[agcData.Count - 1] < agcData[agcData.Count - 2])
                                {
                                    AxisControl.setMove(0, 0); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("方位跟踪完成"); })); zhuangtai3[1] = 1;
                                    infoData[2] = agcData[agcData.Count - 1];
                                    infoData[3] = weizhi[weizhi.Count - 1];
                                    this.Invoke((EventHandler)(delegate { WriteInfo("最小AGC为" + infoData[2].ToString("0.00") + ",对应方位角为" + infoData[3].ToString("0.00")); }));
                                    infoDataMax[0] = agcData.Max();
                                    infoDataMax[1] = weizhi[agcData.IndexOf(infoData[0])];
                                    weizhi = new List<double>(0);
                                    agcData = new List<double>(0);
                                }
                            }
                           
                            if (sat == 4) //4方位回最大，
                            {
                                AxisControl.setPosition(0, infoDataMax[1], fwSp);
                                sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("方位向最大AGC运动中..."); }));
                            }
                            if (sat == 5) //5俯仰识别到位
                            {
                                if (Math.Abs(infoDataMax[1] - AxisControl.getPosition(0)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(0, 0);
                                    sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("方位到达最大位置"); }));
                                }
                            }
                            if (sat == 6) //0方位到达扫描起始位置
                            {
                                AxisControl.setSpeed(1, fySp);
                                AxisControl.setMove(1, 2); sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("俯仰跟踪中..."); }));
                                weizhi = new List<double>(0);
                                agcData = new List<double>(0);
                            }
                            if (sat == 7) //2方位识别到位
                            {
                                weizhi.Add(AxisControl.getPosition(1));
                                agcData.Add(agc.GetAGC());
                                if (agcData.Count > 2 && agcData[agcData.Count - 1] < agcData[agcData.Count - 2])
                                {
                                    AxisControl.setSpeed(1, fySp);
                                    AxisControl.setMove(1, 1); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("俯仰换向跟踪中..."); }));
                                    infoData[4] = agcData[agcData.Count - 1];
                                    infoData[5] = weizhi[weizhi.Count - 1];
                                    this.Invoke((EventHandler)(delegate { WriteInfo("最小AGC为" + infoData[4].ToString("0.00") + ",对应方位角为" + infoData[5].ToString("0.00")); }));
                                    weizhi = new List<double>(0);
                                    agcData = new List<double>(0);
                                }
                            }
                            if (sat == 8) //2方位运动到扫描扫描终止
                            {
                                weizhi.Add(AxisControl.getPosition(1));
                                agcData.Add(agc.GetAGC());
                                if (agcData.Count > 2 && agcData[agcData.Count - 1] < agcData[agcData.Count - 2])
                                {
                                    AxisControl.setMove(1, 0); sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("俯仰跟踪完成"); })); zhuangtai3[2] = 1;
                                    infoData[6] = agcData[agcData.Count - 1];
                                    infoData[7] = weizhi[weizhi.Count - 1];
                                    this.Invoke((EventHandler)(delegate { WriteInfo("最小AGC为" + infoData[6].ToString("0.00") + ",对应俯仰角为" + infoData[7].ToString("0.00")); }));
                                    infoDataMax[2] = agcData.Max();
                                    infoDataMax[3] = weizhi[agcData.IndexOf(infoData[0])];
                                    weizhi = new List<double>(0);
                                    agcData = new List<double>(0);
                                }
                            }

                            if (sat == 9) //4方位回最大，
                            {
                                AxisControl.setPosition(1, infoDataMax[3], fySp);
                                sat++;
                                this.Invoke((EventHandler)(delegate { WriteInfo("俯仰向最大AGC运动中..."); }));
                            }
                            if (sat == 10) //5俯仰识别到位
                            {
                                if (Math.Abs(infoDataMax[3] - AxisControl.getPosition(1)) <= dingweijingdu)
                                {
                                    AxisControl.setMove(1, 0);
                                    if (cs <= 1)
                                    {
                                        this.Invoke((EventHandler)(delegate { WriteInfo("跟踪结束！"); })); zhuangtai3 = new int[4] { 0, 0, 0, 0 };
                                        gengzong = false;
                                    }
                                    else
                                    {
                                        this.Invoke((EventHandler)(delegate { WriteInfo("开始下一次跟踪..."); })); zhuangtai3 = new int[4] { 1, 0, 0, 0 };
                                        cs--; sat = 0;
                                    }
                                    sat++;
                                    this.Invoke((EventHandler)(delegate { WriteInfo("俯仰到达最大位置"); }));
                                }
                            }

                            //if (sat == 12) //8俯仰回最大，极化到达扫描起始位置
                            //{
                            //    axisControl.setPosition(2, jhN, jhSp);
                            //    sat++;
                            //    this.Invoke((EventHandler)(delegate { writeInfo("极化向起始位置运动中..."); }));
                            //}
                            //if (sat == 13) //9极化识别到位
                            //{
                            //    if (Math.Abs(jhN - axisControl.getPosition(2)) <= dingweijingdu)
                            //    {
                            //       axisControl.setMove(2, 0);
                            //        sat++;
                            //        this.Invoke((EventHandler)(delegate { writeInfo("极化到达起始位置开始扫描..."); }));
                            //    }
                            //}
                            //if (sat == 14) //10极化运动到扫描扫描终止
                            //{
                            //    axisControl.setPosition(2, jhS, jhSp); sat++;
                            //    weizhi = new List<double>(0);
                            //    agcData = new List<double>(0);
                            //}
                            //if (sat == 15) //11记录最大AGC位置
                            //{
                            //    weizhi.Add(axisControl.getPosition(2));
                            //    agcData.Add(agc.getAGC());
                            //    if (Math.Abs(jhS - axisControl.getPosition(2)) <= dingweijingdu)
                            //    {
                            //        axisControl.setMove(2, 0); sat++;
                            //        this.Invoke((EventHandler)(delegate { writeInfo("极化扫描完成..."); }));zhuangtai3[3]=1;
                            //        infoData[4] = agcData.Max();
                            //        infoData[5] = weizhi[agcData.IndexOf(infoData[4])];
                            //        this.Invoke((EventHandler)(delegate {  writeInfo("最大AGC为" + infoData[4].ToString("0.00") + ",对应极化角为" + infoData[5].ToString("0.00")); }));
                            //    }
                            //}
                            //if (sat == 16) //12极化到位中
                            //{
                            //    axisControl.setPosition(2, infoData[5], jhSp);
                            //    sat++;
                            //    this.Invoke((EventHandler)(delegate { writeInfo("极化到位中..."); }));
                            //}
                            //if (sat == 17) //13极化到位
                            //{
                            //    if (Math.Abs(infoData[5] - axisControl.getPosition(2)) <= dingweijingdu)
                            //    {
                            //        axisControl.setMove(2, 0);
                            //        if (cs <= 1)
                            //        {
                            //            this.Invoke((EventHandler)(delegate { writeInfo("扫描结束！"); }));
                            //            saomiaogenzong = false;
                            //        }
                            //        else
                            //        {
                            //            this.Invoke((EventHandler)(delegate { writeInfo("开始下一次扫描..."); }));
                            //            cs--; sat = 0;
                            //        }

                            //    }
                            //}
                            Thread.Sleep(20);
                        }
                    });
                }
                else
                {
                    gengzong = false;
                    WriteInfo("天线跟踪取消！");
                    button6.BackColor = Color.Silver;
                }
            }
            catch (Exception s)
            {
                MessageBox.Show(s.Message, "消息提示");
            }
        }
        interfaceForm interForm = new interfaceForm();
        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            if (zxBool == false)
            {
                interForm.ShowDialog();
                if (interForm.DialogResult == DialogResult.OK)
                {
                   
                }
            }
            else
            {
                MessageBox.Show("请先取消\"自动执行\"!", "消息框");
            }
        }

    }
}
